Krystalware
Products | Purchase | Free Stuff | Forums | Blog | Testimonials | Company | Contact

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.

posted on Tuesday, March 20, 2007 3:09 PM | Filed Under [ ASP.NET ]

Comments

Gravatar
# re: How to convert an app relative path to an absolute url without ResolveUrl
Posted by Michael
on 7/11/2007 4:55 PM
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.

Thanks for the post!
# re: How to convert an app relative path to an absolute url without ResolveUrl
Posted by Mike
on 7/12/2007 2:53 AM
This solution doesn't work if the path includes a querystring
# re: How to convert an app relative path to an absolute url without ResolveUrl
Posted by Jimmy
on 8/26/2007 9:21 PM
Thanks!
Gravatar
# re: How to convert an app relative path to an absolute url without ResolveUrl
Posted by manovich
on 10/31/2007 7:23 AM
> This solution doesn't work if the path includes a querystring


You can use HttpRuntime.AppDomainAppVirtualPath (http://dotnettipoftheday.org/tips/HttpRuntime.AppDomainAppVirtualPath.aspx)
Title  
Name
Email (never displayed)
Url
Comments   
Please add 3 and 2 and type the answer here: