url rewrite rule does not ignore request for file

2019-07-14 06:39发布

问题:

http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

    <rule name="AngularJS Routes" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

      </conditions>
      <action type="Rewrite" url="/index.html" />
    </rule>

The above is a one of several url rewrite rules widely suggested for angular single page apps. I understand the rule to mean "If the request is not for a physical file, rewrite the url to /index.html.

Excerpt from documentation link above: " This can be used to specify a condition that checks if the requested URL is NOT a file..."

In index.html I have this script reference:

<script src="lib/jquery/dist/jquery.min.js"></script>

This is a physical file and it does exist on the disk in the location specified.
The rewrite rule is picking up this request and rewriting it to /index.html.
Why is that occuring? My web.config is located at the same level as wwwroot.

There are several threads on github related to url rewrites, not sure if this specific issue is covered: https://github.com/aspnet/BasicMiddleware/issues/43 https://github.com/aspnet/IISIntegration/issues/164 https://github.com/aspnet/IISIntegration/issues/192

回答1:

IsFile won't work because the file is not where IIS expects it to be. For an Asp.Net 4 app the index.html file would have been in the site root, but for an Asp.Net Core app the file is in a subdirectory.

Try using the new Rewrite middleware instead, it knows where the files are in the new Asp.Net Core layout. https://github.com/aspnet/BasicMiddleware/blob/dev/samples/RewriteSample/Startup.cs#L16