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

Issue

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?

Solution

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.

Answered By – bkqc

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *