首页 > 解决方案 > JSON 文件的 IIS 10 HTTP 响应 404

问题描述

我有一个不能从 IIS 10 提供的 JSON 文件。当我浏览它时,它总是返回 404.0 错误。我检查了 IIS 日志,请求是正确的路径。该文件位于 Web 目录结构中的指定位置。MIME 类型 .json 已添加为 application/json。我做了这两种不同的方式,使用 IIS 管理控制台,并通过使用删除后跟标签将其添加到 Web Config 来添加它。

我还在具有相同权限的同一目录中添加了其他文件类型、PNG、html。他们加载得很好。JSON 文件名为 translation-en.json。

我已经在 stackoverflow 上搜索了类似的问题,并尝试了我能找到的一切。对于需要使用 fetch 加载文件的组件,我已经与供应商合作。尝试了他们所有的建议。

我最近的尝试是安装 Failed-Request Tracing 并将其配置为记录所有响应 400-999 和详细输出,但我什么也没得到。如果我能做到这一点,我可能会学到一些东西。但没有运气。

Fiddler 仅显示来自服务器的简单 404 响应。

我正在使用 MS URL Rewrite 模块来处理路由。它的配置如下:

    <rewrite>
      <rules>
        <rule name="Static Assets" stopProcessing="true">
          <match url="([\S]+[.](html|htm|svg|js|json|css|png|gif|jpg|jpeg))" />
          <action type="Rewrite" url="/{R:1}" />
        </rule>
        <rule name="ReactRouter Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>

有趣的是,如果我打开目录浏览,我可以看到这些文件:

启用目录浏览

单击 test.png 可以正常工作。单击任何其他 .json 会导致 404 错误。

我完全处于静止状态。帮助?

标签: jsonhttp-status-code-404iis-10

解决方案


这是通过更改 URL 中静态内容的匹配模式来解决的,因此 json 位于 js 之前。

损坏的配置:([\S]+[.](html|htm|svg|js|json|css|png|gif|jpg|jpeg))

工作配置:([\S]+[.](html|htm|svg|json|js|css|png|gif|jpg|jpeg))

完成重写配置部分:

    <rewrite>
      <rules>
        <rule name="Static Assets" stopProcessing="true">
          <match url="([\S]+[.](html|htm|svg|json|js|css|png|gif|jpg|jpeg))" />
          <action type="Rewrite" url="/{R:1}" />
        </rule>
        <rule name="ReactRouter Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>

推荐阅读