views:

2830

answers:

4

What is the best way to use ResolveUrl() in a Shared/static function in Asp.Net? My current solution for VB.Net is:

Dim x As New System.Web.UI.Control
x.ResolveUrl("~/someUrl")

Or C#:

System.Web.UI.Control x = new System.Web.UI.Control();
x.ResolveUrl("~/someUrl");

But I realize that isn't the best way of calling it.

+6  A: 

I use System.Web.VirtualPathUtility.ToAbsolute.

Dave Ward
+1  A: 

I tend to use HttpContext.Current to get the page, then run any page/web control methods off that.

Keith
A: 

@Dave Ward VirtualPathUtility.ToAbsolute() works great, thanks!

travis
+6  A: 

It's worth noting that although System.Web.VirtualPathUtility.ToAbsolute is very useful here, it is not a perfect replacement for Control.ResolveUrl.

There is at least one significant difference: Control.ResolveUrl handles Query Strings very nicely, but they cause VirtualPathUtility to throw an HttpException. This can be absolutely mystifying the first time it happens, especially if you're used to the way that Control.ResolveUrl works.

If you know the exact structure of the Query String you want to use, this is easy enough to work around, viz:

public static string GetUrl(int id)
{
    string path = VirtualPathUtility.ToAbsolute("~/SomePage.aspx");
    return string.Format("{0}?id={1}", path, id);
}

...but if the Query String is getting passed in from an unknown source then you're going to need to parse it out somehow. (Before you get too deep into that, note that System.Uri might be able to do it for you).

jdw