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

如何在IIS单文件夹部署Angular与.NET应用并解决405/404错误

问题原因

你的Angular根目录web.config里的重写规则会把所有非文件、非目录的请求都重写到根目录(/),包括/rest/api/Config这类API请求。这导致原本应该交给子目录里.NET应用处理的API请求,被转到了Angular的入口页面,而Angular入口无法处理API的HTTP请求方法,所以返回405错误。

移除Angular的web.config后,重写规则失效,API请求能正常到达.NET应用,但Angular单页应用刷新时,IIS会尝试找对应路由的物理文件,找不到就返回404,这是单页应用的典型问题。

修复方案

修改Angular根目录的web.config,在重写规则里添加排除/rest开头路径的条件,让这类请求直接透传给子目录的.NET应用。修改后的配置如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
  <rewrite>
    <rules>
      <rule name="SkipAPIRequests" stopProcessing="true">
        <!-- 匹配所有/rest开头的请求,直接跳过后续规则 -->
        <match url="^rest/.*" />
        <action type="None" />
      </rule>
      <rule name="AngularRewrite" 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="/" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>
</configuration>

另外,你.NET子目录的web.config里设置了inheritInChildApplications="false",这个是正确的,能避免父配置干扰子应用的设置,无需修改。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 03:54:50