asp.net and Visual studio root directory question - asp.net

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!

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

../ 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

../ 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.

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.

Related

Why IIS doesn't handle relative path the same way IIS Express does?

When you are testing a web application in visual studio using IIS Express, you'll be able to add a reference to a script file like this (inside an ASPX file):
<head>
<script src="/Javascript/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script>
</head>
However, when you are deploying the exact same application on an standard IIS server (using a web deployment package), you'll get an missing error 404 because the browser won't be able to resolve the url.
To fix it, I could simply remove the slash from the path:
<script src="Javascript/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script>
However, this question ain't about how to fix the link since this question has been asked and answered many time. What I would like to understand is why does IIS Express handle those url correctly while IIS Express ain't able to? Is it question of security (ex: browser has access to you local file while working on your own computer which ain't the case on remote server) or is it a matter of settings the doesn't get exported in the web package?
From what I can tell from the answer you made in the comments, I understand that your IIS server is configured with something like http://localhost/mysite/Javascript/... while your IIS Express setup is more like http://localhost:8888/Javascript/...*
If it is really the case, the difference comes from the fact that a relative URL beginning with / means it is relative to the host name part of the url while a relative url without the initial / is relative to the actual page folder.
In the current example, if the page is demo.htm,
Server
Base Page
Relative Url
Result Url
IIS
http://localhost/mysite/demo.htm
/Javascript/jquery-2.1.1.min.js
http://localhost/Javascript/jquery-2.1.1.min.js
IISExpress
http://localhost:8888/demo.htm
/Javascript/jquery-2.1.1.min.js
http://localhost/Javascript/jquery-2.1.1.min.js
IIS
http://localhost/mysite/demo.htm
Javascript/jquery-2.1.1.min.js
http://localhost/mysite/Javascript/jquery-2.1.1.min.js
IISExpress
http://localhost:8888/demo.htm
Javascript/jquery-2.1.1.min.js
http://localhost/Javascript/jquery-2.1.1.min.js
Since I suppose your Javascript folder is part of your site, as you can see in the previous table, a Url with the front slash would not point at the right Url. On the other hand, using the front slash to access a root folder would work from anywhere within a website.
On a site not, another way of having both behave the same way would be to modify the project's properties for IIS Express (or directionly the applicationHost.config file) so that it also uses a virtual directory instead of mapping directly to root.

ASP.NET MVC in Subfolder/Virtual Directory/Routing/Domain issue

I posted the following question yesterday but didn't get a helpful answer.
May i was not clear enough, i am very sorry.
After hours of searching here and googling with Bing i could not solve
the following
Problem. It does not should a "heavy" Problem.
here is my problem
I have a Domain like http://example.com that is pointed to a
virtual directory on my webspace like
http://myprovider.com/VirtualDirectoryName/.
The folder structure on my webspace is (off course) like this
root
bin
VirtualDirectoryName
bin
Content
Scripts
Views
Web.config
Global.asax
If i call now "http://www.domain.com" i get the Site without CSS and the
links
are "http://www.domain.com/VirtualDirectoryName/".
I used the default and almost empty default "Internet Application". So
the routing
is default and all links are created with #Html.ActionLink.
Please help me, i tried so many things but i cant get it working. In my
local environment i can affect that with IIS settings but (off course) i
do not have access on the IIS settings of my host.
btw, my hosting provider provider is discount ASP.NET.
Thank you all!
How are you referencing the css files in your html?
If your referencing the like:
<link href="../../Content/Style.css" rel="stylesheet" type=\"text/css\" />
It might not be looking in the correct directory. Try the referencing it as the following:
<link href="/Content/Style.css" rel="stylesheet" type=\"text/css\" />
That way you reference it from the root.
A good way to check is to use the "net" tab within firebug this will show if they are loading and if not what location it is trying to find them.
Link your css and javascript this way (using ResolveClientUrl):
<link href="<%=ResolveClientUrl("/Content/style.css")%>" rel="stylesheet" type="text/css" />
The problem was a simple routing error. My Application was only a simple folder in the iis and not a virtual directory / application. After setting the folder to an application the problem is gone.
I think you can solve it using web.config at the root as suggested here How to host ASP.NET MVC site in a subfolder

CSSFriendly issue just after publishing to web server

I created a website and used css friendly adapters for Menu, TreeView and GridView, all was correct, but after publishing it to my great web server, the menus and treeviews didn't load and there is just some bullets!
Here is my work: http://jds.cot.ir (dead link)
Left side I have a menu which did not load.
Did you copy over the App_Browsers folder? I'd check you copied all files over and, if you can, restart the application pool (touching web.config should achieve this).
I found the problem and the solution, I have an error loading one css file:
<link href="/WebResource.axd?d=FmPs0x8PbK0cHdhnI4N-J7cB33HdEr5UOoA_QzdIwqZdeINM8Kod5dxru5SzZMkL0&t=633820044382031250" type="text/css" rel="stylesheet" />
and the content of this css was an error titled:
Specified argument was out of the range of valid values. Parameter name: utcDate
I changed my pc date to one month ago and rebuilt my solution, and upload it again.
And every thing is correct. ;)
Try deleting precompiled.config from the root of the web app on the server. That did it for me. Haven't really looked into what the pros and cons are. Looks like the publisher's precompilation doesn't take into account the control adapters for some reason. I just ran into this in VS2010 beta 2.

CSS file not getting downloaded in Visual Studio 2008 SP?

This might sound a little wierd, but all of a sudden the CSS and Javascript files referenced in my master page are not being downloaded while the page is being rendered. I am working on a ASP.NET MVC project and things were all fine like half an hour ago!
Here is what I have in head section of the master page,
<link href="/Content/MyCSS.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
I can see the CSS class intellisense while designing pages. But in the page source I do not see these files being added. I can see the css being applied in the VS designer. I have tried restarting VS, restarting my machine too.
Anyone else faced this situation before. I might go crazy now.
Sounds odd. I would use Microsoft Fiddler to see if that gives you any clues. Also are you using IIS or the built in Web server?
If you are using some kind of source control, you might also want to review your recent changes.
Check that your paths are correct. You can create a web app in a virtual directory off the root, so you path would be "/myapp/default.aspx", in which case your paths for your css and js would be wrong.
Try to avoid putting absolute paths in file references.
You may want to try and remove the leading slash, for example.

ASP.Net error: "Cannot use a leading .. to exit above the top directory"

I'm seeing this error several times an hour on my production site and am not quite sure how to fix it. I've grepped the source code and I am not using "../" anywhere in my code to generate a path.
My application is running on IIS6 on Win2003 Server. It's using URLRewriter.Net to allow the site to have friendly URLs, and I'm wondering if this could be contributing to the problem.
I've already Googled for a solution and have found several possibilities but none have worked for me.
I've tried creating a new App_Browser file to force the tagwriter settings to use a specific HtmlTextWriter, as suggested in one of the most popular solutions, but that hasn't worked for me. I haven't seen any other likely solutions.
It's probably due to using "~/something", probably on a Hyperlink control. When the physical file is at a different directory level from the friendly URL, ASP.NET uses too many ../'s in the relative URL that it generates, giving this error. If you can't just use an absolute URL instead, I believe that you can use Page.ResolveUrl("~/foo/bar") to get the proper relative URL.
Just set the Enable Parent Paths to True in the ASP settings from your website
That solved the problem for me
Use reflector to dig into the URLRewriter.Net code.
I would give your rewrite paths another once through and see if maybe there is a path that contains a folder that either doesn't exist, has been flubbed or is in fact outside the web application. Can you post the rewrite rule here?
Are you sure that you are causing the error?. Some scripts (Code Red/Nimda) troll the internet looking for a URL traversal that looks like that error.
Is this internal or external? Is it possible that your machine is getting scanned?
I was able to resolve this issue by installing .NET 3.5 SP1.

Resources