React Router Dom 5 - 从 IIS 服务器下载静态文件。如何配置web.config文件?

问题描述 投票:0回答:1

React 应用程序与下载文件位于同一目录中。

https://some-url.com/ - 使用 React-router-dom 5 的应用程序 https://some-url.com/files/ - 包含静态文件的目录,位于服务器上。 有时路由器没有时间处理地址并收到 404 错误,并且文件无法下载。 (因为缓存什么的)

如何配置 web.config 服务器文件,以便您可以从文件夹 site.com/files/ 下载文件?

web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Static Assets" stopProcessing="true">
          <match url="([\S]+[.](html|htm|svg|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>
  </system.webServer>
</configuration>
reactjs iis react-router-dom
1个回答
0
投票

请尝试以下配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!-- Rule for static assets -->
        <rule name="Static Assets" stopProcessing="true">
          <match url="([\S]+[.](html|htm|svg|js|css|png|gif|jpg|jpeg))" />
          <action type="Rewrite" url="/{R:1}" />
        </rule>

        <!-- Rule for the /files/ directory -->
        <rule name="Files Directory" stopProcessing="true">
          <match url="^files/(.*)" ignoreCase="true" />
          <action type="Rewrite" url="/files/{R:1}" />
        </rule>

        <!-- Rule for React Router routes -->
        <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>
  </system.webServer>
</configuration>
© www.soinside.com 2019 - 2024. All rights reserved.