How to use app-relative paths in ASP.NET – level 100

I’ve been using this feature since the beginning, but I’ve come to realizethat a lot of people aren’t aware of this. The application path shortcut:“~/”

When including any resource in your page whether it be a user control,image, css or whatever, it has to be located. The simplest thing to do isto type the entire path, http://localhost/myApp/myStyle.css, but that isn’tvery maintainable because when you deploy, you have to change thisreference. So the next step is making the path relative to the page (buried2 levels deep): ../../myStyle.css, but what if the site structure changes?What do you do? You could make it domain-relative: /myApp/myStyle.css, butwhat if your application needs to be deployed to it’s own domain:http://myAppDomain.com/ ? Now this reference is broken. Some people getaround this by setting the path with Request.ApplicationPath +“/myStyle.css”, but this doesn’t work in all cases. If you have a virtualpath, you get /myApp, but if you are at the root, you get “/”, so in thecase of /myApp, you have to add a “/” to the end.

To get around all this, use “~/” as a shortcut to you application path.With that shortcut at the beginning of your path, any server control willresolve the url for you. If you use this in a non-server control or justprint it out, you will need to resolve it yourself using the ResolveUrl(…)method of the Control class. This method is inherited by all controls, soyou can use it directly in Page, UserControl and all web controls. I usenothing but app-relative paths now.

In .Net 2.0, we’ll get more App-relative shortcut properties likeRequest.AppRelativePath. It’s really a much cleaner way to represent aresource relative to the application no matter what the path to theapplication is.

Related

VB
7:07 am on November 23, 2004

Thanks, I try with reporting service but it didn’t work. I work on relative path for logo picture. If you know the way around, let me know. Appreciate.

John
10:32 am on February 1, 2005

I’ve been looking into app-relative paths for my .NET application. The tilde seems to work well at runtime, but it does not resolve in the designer. For example, <img src="~/images/myimage.gif"> works great at runtime, but in the designer for the aspx page, the image comes up broken. Likewise, if I try to use <%=ResolveUrl("~/styles.css")%> for my stylesheet path. Have you found a way around this problem?

Jeffrey Palermo
11:25 am on February 1, 2005

That’s right. This is runtime functionality. They will not resolve in the designer. This is not a "problem" but the intended design. I don’t use the designer much anyway. I don’t like the code it produces. I prefer the markup or code view.

John
11:50 am on February 1, 2005

Thanks for the quick reply. The project that I am working on relies on grid layout, therefore, I need to find a designer friendly solution.

Jeffrey Palermo
12:50 pm on February 1, 2005

I would recommend posting this question to an ASP.NET newsgroup. Maybe a designer guru will have a solution.