首页  >  问答  >  正文

使用 NET7 api 反应应用程序 - 在一个站点上重写 URL

我正在尝试在一个 IIS 站点上托管具有 NET7 api 的 React 应用程序。根文件夹中有这样的文件结构

Api 可以工作,因为我请求 /api/status healthcheck 方法,它返回 200。但是当我请求 /index.html 时,我得到 404 (未找到)

你知道我应该如何设置重写规则或以其他方式配置它来获取 index.html 文件

这是我的 web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\api\my-app.exe" arguments=".\api\my-app.dll" stdoutLogEnabled="true" stdoutLogFile=".\iis-logs\" hostingModel="OutOfProcess" />
        <directoryBrowse enabled="false" />
        <httpErrors errorMode="DetailedLocalOnly" existingResponse="Auto" />
        
        <rewrite>
            <rules>
                <clear />
                <rule name="Stop process React routes" stopProcessing="true">
        TODO: how to write rule to get index.html file ??
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

P粉752826008P粉752826008254 天前293

全部回复(1)我来回复

  • P粉564301782

    P粉5643017822024-01-11 10:54:05

    您可以尝试修改web.config文件中的重写规则。以下是如何设置重写规则来服务 React 应用程序的 index.html 文件和其他静态资源:

    <rewrite>
             <rules>
                 <rule name="Serve React App" 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>

    您需要根据您的具体项目结构和需求调整路径和配置。

    回复
    0
  • 取消回复