Click here to monitor SSC

Caffeine Induced Tirades about .NET and Life

Resolving an App-Relative URL without a Page Object Reference

Published 19 December 2010 5:38 am

If you’ve worked with ASP.NET before then you’ve almost certainly seen an application-relative URL like ~/SomeFolder/SomePage.aspx.  The tilde at the beginning is a stand in for the application path, and it can easily be resolved using the Page object’s ResolveUrl method:

string url = Page.ResolveUrl(“~/SomeFolder/SomePage.aspx”);

There are times, however, when you don’t have a page object available and you need to resolve an application relative URL.  Assuming you have an HttpContext object available, the following method will accomplish just that:

public static string ResolveAppRelativeUrl(string url)
     return url.Replace("~", System.Web.HttpContext.Current.Request.ApplicationPath);

It just replaces the tilde with the application path, which is essentially all the ResolveUrl method does.

2 Responses to “Resolving an App-Relative URL without a Page Object Reference”

  1. richard_deeming says:

    First, the ResolveUrl method is declared on Control, not Page, so any control instance will work.

    Second, if you’re only interested in app-relative URLs, you can use System.Web.VirtualPathUtility.ToAbsolute instead:

  2. Damon says:

    Ooooh. Thanks for the tip Richard!

Leave a Reply

Blog archive