Irregular expressions regularly

Request.ApplicationPath - may cause unexpected results when using in pages at root level...

One of the first .NET tricks I learnt was from IBuySpy; they showed me how I could sneakily use Request.ApplicationPath so that my sites could seamlessly work when I'm developing against different domains (such as Localhost in development). To perform the "trick" you simply prepend <%= Request.ApplicationPath %> to the beginning of any paths in your pages, such as:

<img src='<%= Request.ApplicationPath %>/Images/Foo.gif' ... />


The problem is that, when a file is requested, it is returned "root-relative" without a trailing slash. This means that when a page is requested from the root folder of a Website, Request.ApplicationPath returns '/'. In the above example the following string will be written out:

<img src='//Images/Foo.gif' ... />


This wouldn't be picked up when developing against localhost because the application will be one level under the root of the Website meaning that the virtual directory name will be returned after the slash '/':

<img src='/ApplicationName/Images/Foo.gif' ... />


However, if a page is requested from a page in a sub-folder, then the path is returned as "/FldrName" and the above example will resolve correctly as:

' From the root
<img src='/FldrName/Images/Foo.gif' ... />
' In a virtual directory
<img src='/ApplicationName/FldrName/Images/Foo.gif' ... />


So, is there a single answer for developing on root and virtual directories - I'm not sure. The easiest way is to simply add all of your pages under a sub-folder (which I believe is done in the case of IBuySpy). That's too bad if you've already got a site with all of its pages in the root folder though :(

Posted: May 17 2004, 10:05 PM by digory | with 19 comment(s)
Filed under:


pods said:

This often bothers me to. Most of my projects are being published on root when they go to production. But on development machine I do not run Windows Server, therefore only cannot create more than one site. Respectively applications must run in virtual directories. I have not found yet "the shortest and most elegant" statement of combining ApplicationPath with path relative to application root. I would never suggest using ApplicationPath + MyPath. ...could be(Request.ApplicationPath.Lengt == 1 ? path : Request.ApplicationPath + path). Does not look great either
# May 17, 2004 7:32 AM

Paul Wilson said:

Just use ~ and add a runat=server and it will work for images and hyperlinks.
For stylesheets you have to do a little more work, but ~ with ResolveUrl works.
# May 17, 2004 8:11 AM

Darren Neimke said:

Thanks Paul... that's exactly what I'll do!
# May 17, 2004 8:12 AM

Scott McCulloch said:

You can also use Page.ResolveUrl("~/somelink.aspx") as Paul mentioned


Control.ResolveUrl('~/somelink.aspx") to have it relative from a controls physical position, (very useful in developing dotnetnuke modules!)
# May 17, 2004 8:32 AM

AndrewSeven said:

Among other considerations, I don't like to see <%= Request.ApplicationPath %> in the html.
In every project, we have the virtual root in a property of the base class or a "Settings" object.
We also plan it so that paths are always expressed the same way, no slash before the foldre: src='<%=VirtualPath%>Images/Foo.gif'
# May 17, 2004 9:18 AM

Scott Mitchell said:

Control.ResolveUrl(...) works well. If you need this functionality in an HTTP module or handler, which doesn't have a page instance, you can always use Reflector to view the source code of ResolveUrl() (it's only a few lines), and off you go! :-)
# May 17, 2004 11:36 AM

GM said:

If you are using, this is an option

Response.Redirect(Request.ApplicationPath + Path.Combine("/", "Register.aspx"));

# January 22, 2008 4:34 AM

Mike said:

A little know one is:


It has the advantage of not requiring a context.

# February 17, 2008 9:45 PM

cr said:


Great, thanks

# March 10, 2008 8:40 AM

Trick said:

I use this:

string url = this.Request.ApplicationPath.TrimEnd('/') + "/page.aspx";

# June 11, 2008 5:48 AM

rickduarte said:

I'm using this on my HttpModule:

app.Request.Url.OriginalString.TrimEnd(app.Request.Url.PathAndQuery.ToCharArray()) + "/Default.aspx"

# July 29, 2008 4:17 PM

Scott said:

just use


# October 8, 2008 3:53 AM

Deepak said:

I liked Mike's way and modified it so that you don't  need to put bracket around whole thing

<a href="<%=VirtualPathUtility.ToAbsolute("~/")%>Images/Foo.gif">

I use to use this function

   public static string getPath()


           if (HttpContext.Current.Request.ApplicationPath != "/")

               return HttpContext.Current.Request.ApplicationPath + "/";


               return HttpContext.Current.Request.ApplicationPath;


# April 16, 2009 6:41 AM

smith said:

still a great page - VirtualPathUtility is one of those fast forgotten "Oh Yeah, that was it!" pieces for those of us previous-experts returning to work

# March 3, 2010 3:05 AM

ipad app said:


"Hey - great blog site. Just checking out some weblogs, seems a fairly awesome platform you're utilizing. I'm currently using Wordpress for a couple of my blogs but I am not happy with it so significantly. I'm hunting to alter 1 of them more than to a system identical to yours (BlogEngine) like a trial operate. Something in distinct you would recommend about it?"

# January 4, 2011 7:40 AM

ipad reviews said:


"Aw, this was a seriously top quality post. In theory I'd prefer to jot down similar to this as well - taking time and real work to create a excellent write-up... but what can I say.!!!. I  procrastinate alot and in no way appear to obtain something carried out.   "

# January 9, 2011 8:19 AM

apan said:

System.Web.HttpContext.Current.Request.ApplicationPath.TrimEnd(new char[] { '/' }) + "/img";

# February 11, 2011 1:35 AM

Marilyn Oliver said:

hank for this excellent internet log! I completely appreciate it!

# June 30, 2011 10:37 AM

itman1981 said:

<%=HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath%>

# April 4, 2012 4:25 AM