Select correct path for files from App Under IIS

2019-07-25 23:22发布

问题:

I have inherited an .NET app that is hosted on IIS. Previously it would have been its own site within IIS. Now in my Dev environment I have to run it under Default WebSite (in Production it will still run as its own site.

So in Dev I was having problems loading scripts and css files - so I right click Default Web Site in Dev and Add Application - call the new App - MyApp and point to the physical location on disk.

So previously style sheets would have been loaded:

href="/css/folder/mystylesheet.css"

and js files:

src="/jslib/jquery-1.9.1.js"

which worked fine when the application is hosted as its own site within IIS and which still needs to happen in Production (just not in my dev) So in dev I need to change the css and js as below:

href="~/css/folder/mystylesheet.css" (note ~ added)

src="./jslib/jquery-1.9.1.js" (note . added)

and now the js and css files for MyApp under Default website are loaded. However is there something simple I can do in IIS or webconfig to switch this behaviour on/off easily in Dev/Production rather than editing all the places where scripts and css files are loaded - as I know at some point a file will get checked into Production with the path incorrect

The other problem is there is numerous links throughout the site that are all relative so were /Link/Page.aspx which now break in MyApp hosted under Default Website

EDIT

Looking at the answer below from this question

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

All my paths start with / (i.e going to server root) - what I need to figure out is there something I could add to web.config that for Dev would let me specify The Virtual Application MyApp needs to be taken into account (just for Dev - this could then be removed in Web Transform for Production web.config file

回答1:

The short answer is no, there is not one single place where you can deal with the site's location. That's because there are many possible outcomes that you might want, including:

  • site served from IIS root folder and accessed at root of domain (or not)
  • site served from IIS subfolder and accessed at same path (or not)
  • IIS rewrite rules (inbound and outbound), which adds layers of mapping between public and private paths
  • sites mapping to subdomains, DNS wildcard mapping (where requests are distinguished by hostname), etc
  • sites mapping to child applications, inheriting or not inheriting rewrite rules (and other configuration)
  • all of the above

At the same time, there are many places where you interface with paths:

  • internal physical paths, usually for disk access, and usually requiring mapping of virtual path
  • internal virtual paths
  • internal ASP.NET paths, like virtual paths except that they can be app-relative (using ~), which IIS doesn't always recognize
  • public-facing paths
  • all of the above can be relative or absolute

So yeah, it's a mess.

For your case, the simplest thing is probably to make the development environment mirror the production environment. Unless you're moving the live site, delegating all the path references to a configuration-aware function is probably more trouble than it's worth.