How to use absolute url in aspx page on development machine?

1.4k Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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

1
On

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);
}