我们在 Azure 上有一个网站,使用用 C# 编写的 REST 后端和用 Angular2 编写的前端。
当我单击网页中的菜单或按钮时,会打开正确的页面,并且 URL 将从 <mydomain>/
更改为例如<mydomain>/client/16
这工作得很好,如果我理解正确的话,Angular2 路由就会处理这个问题。
但是当我在这个页面 <mydomain>/client/16
并按 F5 刷新浏览器,我收到 404 错误:The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
如果我理解正确的话,这是由 IIS 路由处理的。
我几天来一直在阅读有关此问题的帖子,并尝试了一些建议,主要是向 Web.config 添加重写规则,但到目前为止没有任何效果。
在本地,我们没有这个问题,因为我们在 Visual Studio 中运行后端,在 NPM 中运行前端。这使得调试和解决非常困难。
我应该怎么做才能解决这个问题?
最佳答案
在 IIS 论坛上,我被指向 How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode?
将此规则添加到我的 Web.config 解决了我的问题:
<rule name="AngularJS Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
我以为我以前尝试过这个,但显然没有完全尝试过;)
关于c# - 使用 Angular2 进行 Azure IIS 路由 --> 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40038776/