设置 IIS 路由以允许 Angular 路由工作

Setup IIS Routing to allow Angular Routing to work

我在 IIS 服务器 中部署了一个 angular 应用程序(前端应用程序)并部署了一个 asp.net 网络 API 应用程序(后端应用程序)在 另一台服务器 上(使用 不同的服务器 angular 应用程序和 asp.net 网络 API 应用程序)。

当我转到 angular 应用程序时 URL 我可以访问我的网页。当我点击菜单项时,angular 路由器将我路由到请求的 URL,但是当我在浏览器 中输入 requested URL 时,我得到一个 IIS 404 not found错误。

我不知道是什么问题。

IIS 正在尝试路由您的请求。将 IIS 设置为重定向到 root。 IIS 重定向到 root 后,Angular 路由将选择路由的其余部分。

这是我 Web.config 中的内容。

<rule name="Force Index.html" enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{REQUEST_URI}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="/" />
</rule>

发生这种情况是因为 IIS 对客户端路由一无所知,因此,您必须编写一条重写规则,规定始终将未知 URL 的流量定向到 Angular 应用程序的索引文件。 Angular 完成后将负责提供正确的路线。