簡體   English   中英

在IIS上托管Angular2應用程序后,直接url無效

[英]After hosting Angular2 app on IIS direct url is not working

我們開發了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應用程序中我需要做的任何事情?

任何人都可以建議我如何解決這個問題?

我通過以下步驟解決了這個問題。

  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> 

  1. 在index.html中設置基本標記。 它應該在<head>標簽之后立即顯示

 <base href="/"/> 

這三個步驟將指導您在IIS 7.5或IIS 8.0上使用html5mode url部署Angular2 / Angular App

希望這會有所幫助。 我必須通過幾個答案來解決這個問題。

使用Angular 4.0和IIS 6.1,更簡單的更改對我有用。 這允許我使用命名站點而不是默認文件夾或上面的重寫。 我剛剛將基礎href從'/'更改為我的app文件夾的名稱:

<head>
    <base href="MyApp">
...

希望這對其他人有用!

[windows hosting]創建文本文件並將其命名為“ 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>

最好的運氣:)當我節省別人的時間時,這是有益的!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM