url 重写规则不会忽略对文件的请求 [英] url rewrite rule does not ignore request for file

查看:25
本文介绍了url 重写规则不会忽略对文件的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>

以上是 Angular 单页应用程序广泛建议的几种 url 重写规则之一.我理解规则的意思是如果请求不是针对物理文件,请将 url 重写为/index.html.

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.

摘自上述文档链接:这可用于指定检查请求的 URL 是否不是文件的条件..."

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

在 index.html 我有这个脚本参考:

In index.html I have this script reference:

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

这是一个物理文件,它确实存在于指定位置的磁盘上.
重写规则接收这个请求并将其重写到/index.html.
为什么会这样?我的 web.config 与 wwwroot 位于同一级别.

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.

github 上有几个与 url 重写相关的线程,不确定是否涵盖了这个特定问题:https://github.com/aspnet/BasicMiddleware/issues/43https://github.com/aspnet/IISIntegration/issues/164https://github.com/aspnet/IISIntegration/issues/192

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

推荐答案

IsFile 将不起作用,因为该文件不在 IIS 预期的位置.对于 Asp.Net 4 应用,index.html 文件应该位于站点根目录中,但对于 Asp.Net Core 应用,该文件位于子目录中.

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.

尝试使用新的 Rewrite 中间件,它知道文件在新的 Asp.Net Core 布局中的位置.https://github.com/aspnet/BasicMiddleware/blob/dev/samples/RewriteSample/Startup.cs#L16

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

这篇关于url 重写规则不会忽略对文件的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆