MVC bundles and IIS virtual directories (URL rewri

2019-04-23 18:03发布

问题:

I'm hosting multiple applications on IIS server virtual directories and I'm using URL Rewrite to facilitate them. All images and other assets that are manually written like this "~/path/to/my/content" has a correct output "/path/to/my/content", but bundle paths like "~/client/js" gives an output "/myapplication/client/js" which should be "/client/js".

How can I fix that?

How I initiate Script bundle:

var scriptBundle = new ScriptBundle("~/client/js");

Rewrite configuration:

<rule name="Official Website" stopProcessing="true">
               <match url="(.*)" />
               <conditions>
                  <add input="{HTTP_HOST}" matchType="Pattern" pattern="^(www\.)?domain\.com$" ignoreCase="true" negate="false" />
               </conditions>
               <action type="Rewrite" url="officialsite/{R:1}" />
            </rule>

Was looking into these topics, but couldn't make anything work for me:

  • How do I get System.Web.Optimization bundles to work with custom folders in an IIS virtual directory?
  • CssRewriteUrlTransform with or without virtual directory
  • Why does ResolveBundleUrl not work for custom folders? (MVC Beta 4)
  • Is it possible to unit test BundleConfig in MVC4?

UPDATE: I'm using Winhost as hosting provider, and they do not support setting up host headers for the IP, probably due to the shared IP. They provide domain pointers to root folders, which is why I'm using URL rewrite.

回答1:

You said :

I'm hosting multiple applications on IIS server virtual directories and I'm using URL Rewrite to facilitate them

So, The problem refers to root configuration (Virtual Directory) to be usable for bundling and etc.., It's not about bundling configuration or rewrite rule. I think you need to setup myapplication directory as virtual and set it up as a separate website. Make sure you have followed instructions available in Create Web Site where it says:

you add a Web site in IIS, a site entry is created in the ApplicationHost.config file. The entry specifies:

  1. network binding for the site
  2. Maps the site to a location in the file system
  3. optionally specifies user credentials for content access

To ensure that user requests reach the correct Web site, you must configure a unique identity for each site on the server.Web sites hosted on the same server can be distinguished using the following unique identifiers.

  • Host header name (recommended)
  • IP address
  • TCP port number

Configure a Host Header for a Web Site (IIS 7) points to the first one.

Update: I think there may be a name conflict on server and your project. more brightly try to change client in ~/client/js and its folder to new one. Unless there were multiple path choices (with this path depth) for server to fetch that you can solve it by adding an extra depth by adding an \ in your project and js bundle mapping.

Update2: Finally I suggest you to follow full article available in How to publish or host subdomain on winhost.com? to solve such this amazing issue :)



回答2:

I have two options you can try.

1) Try using this to trick it into using the correct path:

var scriptBundle = new ScriptBundle("~/../client/js");

You may get an HttpException (Cannot use a leading .. to exit above the top directory.), but it's worth a shot. I cannot replicate your environment, so I cannot test.

2) Create your own virtual path provider: Changing ASP.net application root?