0

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?

ak3nat0n
  • 6,060
  • 6
  • 36
  • 59

2 Answers2

1

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
  • 187,200
  • 47
  • 362
  • 445
-1

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

ak3nat0n
  • 6,060
  • 6
  • 36
  • 59