How to convert an app relative path to an absolute url without ResolveUrl
March 20. 2007 4 Comments
- Posted in:
- ASP.NET
Many times I've looked for a way to convert an app relative url (such as one using the ~/ app root notation) to an absolute path without ResolveUrl. If the code is executing outside a Control, for example in an IHttpHandler or business layer code somewhere that has no reference to a Control, you can't call Control.ResolveUrl.
Today, I discovered a way to do this without a Control reference. The System.Web.VirtualPathUtility class has some very useful methods for manipulating paths, including one that converts from an app relative path to an absolute path. Instead of Control.ResolveUrl, just call VirtualPathUtility.ToAbsolute:
string url =VirtualPathUtility.ToAbsolute(url);
Viola! An app relative path to an absolute path in one line of code without using Control.ResolveUrl.
Comments
Thanks!
Jimmy> This solution doesn't work if the path includes a querystring
manovichYou can use HttpRuntime.AppDomainAppVirtualPath (dotnettipoftheday.org/.../...inAppVirtualPath.aspx)
I just wanted to let you know that your post was very helpful. I was not aware of this class and have since been able to make use of it in a couple of custom controls.
MichaelThanks for the post!
This solution doesn't work if the path includes a querystring
Mike