ResolveUrl Method outside of a Page or Control, or in a Static method

This is an update to my original post on this topic:

Nathan offered this alternative to my original utility function posted here:

VirtualPathUtility.ToAppRelative("~/path/to/item");

Now the original post follows:

Sometimes we create Utility classes or Functions that provide path related functionality, and finding or resolving a path relative to the current request may be necessary. In an ASP.NET Page or Control, the ResolveUrl function is available for this. But if you need this functionality in a class in App_Code, for example, it gets a little tricky. The System.Web.VirtualPathUtility class methods could be used to achieve ResolveUrl like functionality, but there is an easier way. You could always get a reference to the current (executing) ASP.NET Page by casting Context.Handler to Page, and then call ResolveUrl on the Page. This should work in most situations because the only time you would need to Resolve Url is when you are running in the context of a Page. So the code would look like the following:

    public static string ResolveUrl(string relativeUrl)
    {
        if (HttpContext.Current != null)
        {
            System.Web.UI.Page p = HttpContext.Current.Handler as System.Web.UI.Page;
            if (p != null)
                return p.ResolveUrl(relativeUrl);
            else
                throw new InvalidOperationException("Unable to Resolve: Not in a Page Context");
        }
        else
            throw new InvalidOperationException("Unable to Resolve: Not in a HttpContext");
    } 

Once you obtain a reference to the current Page via Context.Handler, you can pretty much do everything you could do within a Page.

Posted on August 20, 2008 05:19 by Haider

Comments

August 24. 2008 20:20

Janina

Thank you. It'll help me so..

Janina

September 18. 2008 02:14

Rezgar

Way to go. Thanks, very usefull

Rezgar

April 20. 2009 05:33

Nathan

Nice solution!

Some one just offered me this alternative that is a little closer to how it probably *should* be done as it doesn't depend on a Page object:

VirtualPathUtility.ToAppRelative("~/path/to/item");

Cheers Smile

Nathan

December 1. 2009 01:51

Warren Machanik

Thanks this helped, was pulling out my hair!!!!

Warren Machanik

January 8. 2010 14:03

Haider

VirtualPathUtility.ToAppRelative("~/path/to/item");

Thanks to Nathan above for showing a more appropriate way, I am updating this post.

Haider

May 1. 2010 22:58

Wes

The function you are calling returns: ~/path/to/item

I believe that the function you are trying to call is:

VirtualPathUtility.ToAbsolute("~/path/to/item");

Wes

July 27. 2010 17:08

Miami Hair Restoration

It seems that you've put a good amount of effort into your article and I want a lot more of these on the web these days. I truly got a kick out of your post. I do not have a bunch to to say in response, I only wanted to register to say special work.

Miami Hair Restoration

Don't Post SPAM

If you are posting a commment just to get a link, don't waste your time!

I have a sophisticated comment moderation system in place, and your comment will not be posted.

Add comment




biuquote
  • Comment
  • Preview
Loading