You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 13部署IIS后API请求405错误排查求助

解决同一主机部署Angular 13与REST API时的405方法不允许错误

你的问题出在当前的web.config规则会把所有非文件、非目录的请求都重写到index.html,包括你的API请求(比如/api/token)。而index.html是静态文件,IIS只允许对它使用GET方法,当你用POST等其他方法请求API时,就会触发405错误。

下面是两种配置层面的解决方案:

方案一:在Angular路由规则中排除API路径

修改原有的rewrite规则,添加一个条件,让API开头的请求不执行重写:

<configuration>
<system.webServer>

  <rewrite>
    <rules>
      <rule name="Angular Routes" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          <!-- 添加这一行,排除/api开头的请求 -->
          <add input="{REQUEST_URI}" pattern="^/api/" negate="true" />
        </conditions>
        <action type="Rewrite" url="./index.html" />
      </rule>
    </rules>
  </rewrite>

</system.webServer>
</configuration>

方案二:优先匹配API请求并跳过重写

在Angular路由规则之前新增一条规则,让所有/api开头的请求直接跳过rewrite处理,交给后端API:

<configuration>
<system.webServer>

  <rewrite>
    <rules>
      <!-- 新增API规则,优先匹配并停止后续处理 -->
      <rule name="API Routes" stopProcessing="true">
        <match url="^api/(.*)" />
        <action type="None" />
      </rule>
      <!-- 原Angular路由规则保持不变 -->
      <rule name="Angular 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>

两种方案都能解决问题,方案二更直观,能明确区分API和前端路由的处理逻辑,推荐使用。

内容的提问来源于stack exchange,提问作者Lucas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 01:55:16