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 :(

18 Comments

  • 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.

  • Thanks Paul... that's exactly what I'll do!

  • You can also use Page.ResolveUrl(&quot;~/somelink.aspx&quot;) as Paul mentioned



    and



    Control.ResolveUrl('~/somelink.aspx&quot;) to have it relative from a controls physical position, (very useful in developing dotnetnuke modules!)

  • Abstraction.

    Among other considerations, I don't like to see &lt;%= Request.ApplicationPath %&gt; in the html.

    In every project, we have the virtual root in a property of the base class or a &quot;Settings&quot; object.

    We also plan it so that paths are always expressed the same way, no slash before the foldre: src='&lt;%=VirtualPath%&gt;Images/Foo.gif'

  • 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! :-)

  • If you are using c#.net, this is an option
    Response.Redirect(Request.ApplicationPath + Path.Combine("/", "Register.aspx"));

  • A little know one is:
    VirtualPathUtility.ToAbsolute("~/Images/Foo.gif")

    It has the advantage of not requiring a context.

  • >VirtualPathUtility.ToAbsolute("~/Images/Foo.gif")

    Great, thanks

  • I use this:

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

  • I'm using this on my HttpModule:
    app.Request.Url.OriginalString.TrimEnd(app.Request.Url.PathAndQuery.ToCharArray()) + "/Default.aspx"

  • just use
    Page.ResolveUrl("~/dir/page.aspx")

  • I liked Mike's way and modified it so that you don't need to put bracket around whole thing
    <a href="Images/Foo.gif">
    I use to use this function
    public static string getPath()
    {
    if (HttpContext.Current.Request.ApplicationPath != "/")
    return HttpContext.Current.Request.ApplicationPath + "/";
    else
    return HttpContext.Current.Request.ApplicationPath;
    }

  • 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 asp.net work

  • -----------------------------------------------------------
    "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?"

  • -----------------------------------------------------------
    "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. "

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

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

Comments have been disabled for this content.