views:

252

answers:

2

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?

+1  A: 

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);
}
Daniel A. White
ok, but is there a function to get the 'http://localhost ..' part added to it too?
zaladane
A: 

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

zaladane