Jeffrey Palermo (.com)

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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 realize
that 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 is
to type the entire path, http://localhost/myApp/myStyle.css, but that isn't
very maintainable because when you deploy, you have to change this
reference.  So the next step is making the path relative to the page (buried
2 levels deep):  ../../myStyle.css, but what if the site structure changes?
What do you do?  You could make it domain-relative:  /myApp/myStyle.css, but
what if your application needs to be deployed to it's own domain:
http://myAppDomain.com/ ?  Now this reference is broken.  Some people get
around this by setting the path with Request.ApplicationPath +
"/myStyle.css", but this doesn't work in all cases.  If you have a virtual
path, you get /myApp, but if you are at the root, you get "/", so in the
case 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 will
resolve the url for you.  If you use this in a non-server control or just
print it out, you will need to resolve it yourself using the ResolveUrl(...)
method of the Control class.  This method is inherited by all controls, so
you can use it directly in Page, UserControl and all web controls.  I use
nothing but app-relative paths now.

In .Net 2.0, we'll get more App-relative shortcut properties like
Request.AppRelativePath.  It's really a much cleaner way to represent a
resource relative to the application no matter what the path to the
application is.

Posted Sat, Nov 6 2004 3:34 PM by Jeffrey Palermo

[Advertisement]

Comments

VB wrote re: How to use app-relative paths in ASP.NET - level 100
on Tue, Nov 23 2004 2:07 AM
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 wrote re: How to use app-relative paths in ASP.NET - level 100
on Tue, Feb 1 2005 5:32 AM
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 wrote re: How to use app-relative paths in ASP.NET - level 100
on Tue, Feb 1 2005 6:25 AM
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 wrote re: How to use app-relative paths in ASP.NET - level 100
on Tue, Feb 1 2005 6:50 AM
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 wrote re: How to use app-relative paths in ASP.NET - level 100
on Tue, Feb 1 2005 7:50 AM
I would recommend posting this question to an ASP.NET newsgroup. Maybe a designer guru will have a solution.

http://groups-beta.google.com/group/microsoft.public.dotnet.framework.aspnet
Jeffrey Palermo wrote re: How to use app-relative paths in ASP.NET - level 100
on Fri, May 13 2005 2:07 AM
Thanks! You rock.
Harsh wrote re: How to use app-relative paths in ASP.NET - level 100
on Tue, Dec 6 2005 4:38 PM
Thanks a lot for the article above. I was stuck with the URL not being resolved correctly when using virtual paths with "~". The ResolveURL method of the Control class fixed the problem.
Devlicio.us