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

Comment by Jimmy

Thanks!

Jimmy
Comment by Michael

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!

Michael
Comment by Mike

This solution doesn't work if the path includes a querystring

Mike