views:

243

answers:

5

I have a website that has a relative path to a stylesheet that looks like this "/stylesheets/main.css". Now this works fine when I run the site in Visual Studio. But when I deploy the site to our Windows Server 2003 the path stops working. If I go back into code and change the path from "/stylesheets/main.css" to "stylesheets/main.css", the site works fine on the server. I have another website on a different server that uses the same path style ("/stylesheets/main.css") and stylesheet and works with no problems. I really don't want to change all the paths, and am not even sure if this is a problem with the code or the server. Any help or ideas would be great. Thanks.

+2  A: 

In ASP.NET, many times you will need to use a tilde (~) to get the application's root directory, so your paths would look like ~/stylesheets/main.css

When you specify a path that starts with / you are indicating the server root so if you have you site in a virtual directory, it will not be taken into account, but if the site is hosted as the default site, the path will qualify:

Example: server named foo.net with site hosted in a virtual directory named app /stylesheet will translate to foo.net/stylesheet not foo.net/app/stylesheet

Kit Roed
+2  A: 

Is the site deployed to the domain's root? If the site is at

http://example.com/somefolder/

then the path /stylesheet/main.css will be interpreted as

http://example.com/stylesheet/main.css

rather than

http://example.com/somefolder/stylesheet/main.css

As @Kit indicated, you can work around this by resolving the path to your application's folder. I have often done this in ASP.NET as follows:

<link rel="stylesheet" type="text/xss" href="<%= ResolveUrl("~/stylesheet/main.css") %>"/>

If that's not the problem, you're going to have to give a bit more detail.

harpo
Thanks this did help a lot. But I had to use a combination of different ways on the same site to get it to work. "/default.aspx" to "<%= ResolveUrl("~/default.aspx") %>" "/default.aspx" to "~/default.aspx" url('/images/top.gif') to url('../images/top.gif') And even for one case had to keep the old path type ("/stylesheets/main.css"). This seems very weird to me and I've never had this problem before but I guess it seems like this did the trick.
A: 

It would be better to not use relative path and instead use the full path relative to the server. You can do this by processing the tag of your document at the server. So, something like this:

<head runat="server">
  <link href="~/stylesheet/main.css" type="text/css" rel="stylesheet" />
</head>

The tilde (~) signifies that you are coming from the root directory of the website. The reason this works is because you are processing the head at the server. Note that this will work for any other tag that is processed at the server as well.

JasCav
A: 

A URL path of the form

/path_preceded_by_a_slash

is not a relative path... it's an absolute path, indicating that the path search should start at the DocumentRoot and not the directory that contains the requesting document.

Perhaps that's your problem. You should be using relative paths, but you are not doing it correctly.

Dancrumb
A: 

When using an absolute path (eg. /images/cool_image.gif') , the root folder is the application root folder set by IIS.

Just as a tricky aside, if you are using a Visual Studio ASP .NET Web Application project, the root of your application is set when you set up the Web Application in your IIS or Web Hosting control panel.

If you're using a Visual Studio ASP .NET Web Site project, then the application is compiled automatically 'on the fly' and the root is probably the general IIS root folder, since no application needs to be explicitly set up in IIS. This is unlikely to coincide with the actual root of your application, and for that reason these kind of absolute paths should be avoided in 'Web Site' projects.

For a discussion of Visual Studio 'Web Site' versus 'Web Application', google it - it's a hot topic.

Ben

Ben McIntyre

related questions