1

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?

The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
  • Without seeing the structure of your web _application_ in IIS (guess): your _application root_ is "different" in IIS. – EdSF Jun 01 '16 at 00:14
  • Indeed, IIS Express seem to run the app from the root while a web deploy on IIS will, by defaut, create a application under a subdirectory. Installing in the root directory does fix the problem, but since the path is relative, I would expect it to be relative to the application, not the site. – The_Black_Smurf Jun 01 '16 at 01:27
  • I think you will have to dig further into the configuration files to make sure your test at least is based on equivalent settings. Meanwhile, IIS Express is different from IIS in far too many ways, so I am not surprised. – Lex Li Jun 01 '16 at 02:46
  • In this context it _should be the same_ (IIS / express) - if your site is run/handled by ASP.Net then "root" == Asp.net app root. Check your web deploy settings re: _file system_ sub directory does/should not equate to an _application_ "child". Or check if the root directory of where you deploy is already an application (and if so, don't deploy your new app there)... – EdSF Jun 01 '16 at 13:10
  • Maybe because of difference in "bindingInformation" attribute, in IIS: "*:[port no]:" IIS Express: ":[port no]:localhost". I had something like your issue and resolve it by putting * at first and last value of this tag in IIS Express: "*:[port no]:*" – Mohammadreza Jul 25 '17 at 14:10
  • https://stackoverflow.com/questions/35247847/bad-request-invalid-hostname-asp-net-visual-studio-2015 – Mohammadreza Jul 25 '17 at 14:19

1 Answers1

1

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 side note, another way of having both behave the same way would be to modify the project's properties for IIS Express (or directly the applicationHost.config file) so that it also uses a virtual directory instead of mapping directly to root.

bkqc
  • 831
  • 6
  • 25
  • If I visit: https://abc.xyz.com/emrsso then My Script Path in SSOLogin.ASPX is My URL of website is : https://abc.xyz.com/emrsso I am facing problem Error 404. My script file path is showing in Dev Tool Network tab: https://abc.xvz.com/Scripts/jquery-1.4.1.min.js Which is incorrect as Scripts folder is under emrsso folder on disk. It should be like https://abc.xvz.com/emrsso/Scripts/jquery-1.4.1.min.js If I visit: https://abc.xyz.com/emrsso/SSOLogin.aspx then It rendering properly without any error. – Sup Ravi Kumar May 10 '23 at 06:54
  • Also in Redirection url with response 302, Location is /, which means it should be redirect to https://abc.xyz.com/emrsso, however it is redirecting to URL https://abc.xyz.com. – Sup Ravi Kumar May 10 '23 at 06:57