使用Angular2的Azure IIS路由 - > 404

时间:2016-10-14 08:42:31

标签: c# rest azure iis asp.net-web-api-routing

我们在Azure上有一个网站,它使用一个用C#编写的REST后端和一个用Angular2编写的前端。

当我点击网页上的菜单或按钮时,会打开正确的页面,并将网址从<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中运行前端。这使得调试和解决变得非常困难。

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:3)

在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>

我以为我之前尝过这个,但显然不完全;)