How to use absolute url in aspx page on development machine?
-
11-09-2019 - |
Question
I find myself using the ResolveUrl function a lot in my aspx pages but the resolved path is always relative. i would like to be able to have the rendered path start with the regular "http://localhost/myproject/"
How can i achieve that without breaking any code in case i change the hierarchy of my files? Would it be inefficient to write a function and call it for every targeted link on the page?
Solution 2
Never mind guys, I found some code online from a Rick Strahl post and it seems to be something i might use in my case! thanks for the help
OTHER TIPS
Use the ~
when you resolve the url. It will always go to the application root.
Example
~/somedirectory/default.aspx
will resolve to...
{applicationRoot}/somedirectory/default.aspx
You will need to manually add the server address:
Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port
A sample function would be
string ResolveAbsoluteUrl(string path)
{
return Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port + ResolveUrl(path);
}