在IIS上托管Angular2应用程序后,直接url无效

mic*_*p78 7 iis-7.5 angular2-routing angular-cli angular

我们开发了Angular2 App.

当我们使用'ng serve'在angular-cli下运行时,它工作正常.

一旦我们将应用程序托管到IIS 7.5,我们就可以浏览应用程序的根目录而不会出现太多问题,我们可以导航到创建的应用程序导航中的任何其他视图.

但是当用户尝试加载定位特定路由或组件的URL时,问题就开始了.

因此,如果我们转到http:// serverurl ...它会加载index.html,然后单击index.html上的导航链接,它会将用户带到http:// serverurl/route1/component1

但是当用户尝试通过在浏览器地址栏中键入http:// serverurl/route1/component1来直接转到url http:// serverurl/route1/component1时,它会将该请求发送到IIS,并返回并且错误,而不是资源找到.

并且很容易理解,服务器上不存在url.这是有角度的网址.理想情况下,它应该加载index.html并将其余的url传递给angular路由器并加载/ route1/component1.这适用于'ng serve',但不适用于IIS 7.5.在IIS中我是否有任何设置才能使其正常工作?或者在Anuglar2应用程序中我需要做的任何事情?

任何人都可以建议我如何解决这个问题?

mic*_*p78 23

我通过以下步骤解决了这个问题.

  1. https://www.microsoft.com/en-au/download/details.aspx?id=7435下载的URL重写器模块

  2. 在我的web.config中添加了以下内容

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="AngularJS" 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>
Run Code Online (Sandbox Code Playgroud)

  1. 在index.html中设置基本标记.它应该在<head>标签之后立即显示

这三个步骤将指导您在IIS 7.5或IIS 8.0上使用html5mode url 部署Angular2/Angular App

希望这会有所帮助.我必须通过几个答案来解决这个问题.