views:

52

answers:

4

I am seeing something very odd and thought I would ask the Stackoverflow community if they knew the answer.

I have an asp.net project that runs fine in one environment, but couldn't figure out what happened to the styles in another environment.

In the first environment (Windows 2008 Server), the following link worked fine:

<link href="/Styles/09/style.css" rel="stylesheet" type="text/css" />

but in the other environment (it's a Windows 7), I had to change it to work:

<link href="../Styles/09/style.css" rel="stylesheet" type="text/css" />

Notice that the directories seemed to shift ahead one directory in the Win7, what's going on? It's like the "running" directory seems now be the \bin directory instead of the home!

Which environment is configured correctly? How do I determine execution directory level? My concern going forward is pushing to a prod environment and guessing which configuration is correct.

Any insight would be appreciated!

A: 

are you sure that the hosting settings are correct.

I have had similar issues in the past when gettig ncode to work in IIS instead of the dev servers.

John Nicholas
+2  A: 

../ is almost certainly wrong. That said, if you're having problems across environments the plain / is probably wrong as well. Try using a '~' operator to set an accurate application root.

This link should help you figure out what exactly is going on:
http://msdn.microsoft.com/en-us/library/ms178116.aspx

Joel Coehoorn
The ~ operator only works when you have runat="server"
Nelson
Nelson: that's why it's good to use server controls where you can. ;-)
Parvenu74
I would argue you shouldn't use server controls unless you need to (or is convenient to). Otherwise you are using ASP.NET resources on what is otherwise static text.
Nelson
+2  A: 

The first way is going to search from the root path, the second goes up one directory first. My guess is on the first server your site was hosted at the root (www.example.com/), while on the second server it was hosted in a subdirectory (www.example.com/mysite/). In the second case, it would look at www.example.com/Styles instead of the intended www.example.com/mysite/Styles

Nelson
You are right! Upon checking, the original server had the project hosted at the root. I set up the second, but put it in a separate folder (Ie. http://localhost/CPortal/).I think this is kind of scary having the reference to the styles dependent on where the project is created, do you have any suggestions? '~' is out since this is aspx markup, can't use Server reference.
Mark Kadlec
Figures (I've been there)... ../Styles/ can work, but if you have a master page, the path is relative to the derived pages and often won't work. I believe all regular HTML tags can runat="server", even if they don't have properties. Try <link href="~/Styles/09/style.css" rel="stylesheet" type="text/css" runat="server" />. I seem to recall ASP.NET choking on the CSS though...
Nelson
You may be able to use inline code similar to <link href="<%= ... %>/Styles/09/style.css" /> It would probably be something like Request.ApplicationPath, but I would have to check.
Nelson
Thanks Nelson, this definitely seems like an oversight from the asp.net team though, it would have been nice to have a cleaner way to access the Server side rel path though markup.
Mark Kadlec
Did you try the runat="server" way with the tilde? Otherwise, there may be a better option that I'm not aware of.
Nelson
I just stumbled across this. I haven't tried it, but it might help: http://quickduck.com/blog/2010/01/14/path-resolution-in-asp-net/
Nelson
@Mark - I think I found the "definitive" answer, though I haven't tried it myself yet: http://weblogs.asp.net/scottgu/archive/2006/12/19/tip-trick-how-to-run-a-root-site-with-the-local-web-server-using-vs-2005-sp1.aspx Specifically `<head runat="server">`. This is old, however, so it may be superseded.
Nelson
+1  A: 

../ is just wrong.

/ is site root. If running on webdev, this will be the root of your app. when you move your app to IIS it will be the root of the IIS site, e.g. http://localhost, not the root of your app, http://localhost/yourApp.

As Joel says, using '~' can help aleviate this problem, but the '~' only works on tags that get processed by the page. It isn't going to help you for paths in css or js nor the link tag presented unless you add runat=server .. I think. And even that is not going to help you with static resources like html.

Getting this worked out to provide consistent behavior between your dev and prod environment can be challenging but once you get it you got it.

Sky Sanders
You and Joel are right, but it just seems "hacky" to me to have the styles referenced (they are in a control ascx) and dependent on where on IIS they are deployed.Is there a better way?
Mark Kadlec
@Mark - this is where embedding content and serving webresources comes into play. Believe me, I sympathize but have been coding libraries lately so have nothing on tap as example solution. It is not an impossible situation but aggravating nonetheless.
Sky Sanders