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

时间:2016-08-11 23:09:51

标签: angular iis-7.5 angular2-routing angular-cli

我们开发了Angular2 App。

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

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

但是当用户尝试加载定位特定路线或组件的网址时,问题就开始了。

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

但是当用户尝试通过在浏览器地址栏中输入 http://serverurl/route1/component1 直接转到网址 http://serverurl/route1/component1 时,它会将该请求发送到IIS ,以及找不到资源的返回和错误。

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

有谁能建议我如何解决这个问题?

2 个答案:

答案 0 :(得分:23)

我通过执行以下步骤解决了这个问题。

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

  2. 在我的web.config

  3. 中添加了以下内容

    
    
    <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>
    &#13;
    &#13;
    &#13;

    1. 在index.html中设置基本标记。它应该在&lt; head&gt; 标记
    2. 之后立即生效

      &#13;
      &#13;
      <base href="/"/>
      &#13;
      &#13;
      &#13;

      这三个步骤将指导您在 IIS 7.5或IIS 8.0

      上使用 html5mode 网址部署 Angular2 / Angular App >

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

答案 1 :(得分:0)

[Windows托管] 创建文本文件并将其命名为“ web.config ” 打开它并跳过下面的内容,然后确保它位于您主机的根目录中

<?xml version="1.0" encoding="utf-8"?>
      <configuration>

      <system.webServer>
        <rewrite>
          <rules>
            <rule name="Angular Routes" 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="./index.html" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>

      </configuration>

幸运之星:)当我节省别人的时间时,这是有益的!