Don't panic!

 

Cry about...
C# Development How To Notes

Tilde (~) and Virtual Paths with ASP.NET


Contents:

Introducing the Tilde (~)

In an ASP.NET application the framework controls recognise a tilde ('~') as a shortcut to the root of the application's virtual directory. So the framework will expand a tilde to the value of HttpRuntime.AppDomainAppVirtualPath.

So for example in the code behind

Response.Redirect(@"~/Login.aspx");

will redirect to the page "Login.aspx" in the root of the application and

Response.Redirect(@"~/Admin/Users.aspx");

will redirect to the page "Users.aspx" in the folder "Admin" off the root.

These will resolve to the correct location, regardless of which folder it is called from.

Likewise placing:

<asp:Image ID="image1" runat="server" ImageUrl="~/images/logo.gif"/>

on a page the framework will substitute the correct path to "images/log.gif" where the folder "images" is off the root.

Why use a tilde (~) instead of a relative path?

So why use a tilde instead or a relative path? In many cases you can use a relative path instead of a tilde. Using a tilde is often clearer because it makes it clearer what (where!) is being referenced.

There are also situations where a tilde is the only viable thing to use. This is particularly true when using your own components, controls and class libraries, where with the tilde you can rely on the framework to resolve to the correct relative path.

Outside the framework - Getting the absolute path

There are times when you might need an absolute URL, such as when passing a URL to a non-framework component.

There are a few options:

Option 1. Page.ResolveUrl

Page.ResolveUrl will return a URL relative to the root of the website. So for example:

Page.ResolveUrl(@"~/Admin/Users.aspx")

will return:

/wwwroot/Admin/Users.aspx

where "/wwwroot" happens to be the root of the project I am working in, so that will be different for you.

This can be used for any URLs which need to work within the website.

Option 2. Page.ResolveClientUrl

Page.ResolveClientUrl will return a URL relative to the current page. So for example:

Page.ResolveClientUrl(@"~/Admin/Users.aspx")

will return "Admin/Users.aspx" if called from a page in the root folder, but if it were called from say "Admin/Clients.aspx" - which note is in the same "Admin" folder - then it will return simply "Users.aspx".

This can be used for any URLs which need to work within within the website.

Option 3. Generate an absolute URL

If you are passing a URL to a different website then you may need the full URL. This requires a little bit of code:

public string GetAbsoluteUrl(string page)
{
  if (HttpContext.current == null)
  {
     return page;
  }
  else if (VirtualPathUtility.IsAbsolute(page))
  {
    return
      HttpContext.Current.Request.Url.Scheme + "://"
      + HttpContext.Current.Request.Url.Authority
      + HttpContext.Current.Request.ApplicationPath
      + page;
  }
  else
  {
    return
      HttpContext.Current.Request.Url.Scheme + "://"
      + HttpContext.Current.Request.Url.Authority
      + VirtualPathUtility.ToAbsolute(page);
  }
}

This will return the full URL of the page (or resource) passed to it, making the assumption that the page (or resource) is part of the website and that if the current page is secure then the URL to the page should be secure.



About the author: is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.