Jano's Software Ramblings
Code & Pray from Monterrey

C# Tip #4: Tilde path in Asp.net

September 2, 2007 22:26 by Jano

One of the most useful, but also one of the more poorly documented features of Asp.net is the Tilde (~) the tilde can be use almost anywhere in any url appears on the server control. The tilde is automatically translated to the equivalent of HttpRuntime.AppDomainAppVirtualPath (or Request.ApplicationPath). This give you the path to access your application files from its base, this is really helpful for being able to install anywhere without troubles. It also works on most configuration files in asp.net and when using Server.MapPath. In fact most of the asp.net API that receive Url's are able to run it. 

Usage: 

<img id="myId" src="~/images/image1.jpg" runat="server"/>

turns into:

<img id="myClientId" src="/ApplicationPath/images/images.jpg"/>

 

the runat attribute is important since otherwise the path won't be processed by the server.  


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

October 9. 2007 13:06

Gravatar

Sometimes you need to manually resolve your path with tilde to absolute path but you don't have reference to a Control oject. In this case you can use System.Web.VirtualPathUtility class - http://dotnettipoftheday.org/tips/VirtualPathUtility-ToAbsolute.aspx

Manovich

Comments are closed