關於這個問題,很多問題都是關於這個的。我已經閱讀了很多,他們中的大多數都指向配置IIS重寫的示例web.config文件。最近的帖子到我的問題似乎是這樣的:角路由IIS - 重寫只適用於淺路徑
Angular 2 application not working when moved into IIS virtual directory
然而,這已經下來了投票兩次,似乎是死的,而不是試圖復活它,我想我會在這裏解釋一下我的問題。
代碼託管作爲ASP.NET應用程序:http://127.0.0.1/angular-app/
的角碼通過命令內置:ng build --base-href ./
和所得/dist/
文件夾是在高於本地地址託管代碼。
IIS應用程序有以下web.config文件:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<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>
</rules>
</rewrite>
</system.webServer>
</configuration>
有了這個設置這些URL將返回預期的內容:
http://127.0.0.1/angular-app/dashboard/
http://127.0.0.1/angular-app/heros/
URL重寫規則正確指向應用程序基目錄,因此會找到vendor.js, main.js
等文件。
但是,對於較長的URI,重寫規則似乎只會查看鏈接文件的文件夾路徑的一個備份步驟。例如,URL
http://127.0.0.1/angular-app/heros/20/
將正確返回index.html
並顯示「應用程序加載」
但是挖一個深一點似乎可以嘗試加載vendor.js, main.js
等文件從基本目錄http://127.0.0.1/angular-app/heros/,只有一個步驟,而不是直接從路線。
這些鏈接然後也被重定向回index.html
文件,因此沒有任何腳本被傳遞到瀏覽器。
我一直在使用
<base href="/">
取而代之的是./
,並相應修改重寫規則嘗試,但這總是重定向內容http://127.0.0.1/
正如鏈接後建議的影響,我改變了基本href到:
<base href="/angular-app/">
,這從correc返回index.html
t位置。 然而vendor.js, main.js
,仍然被從服務器的根目錄要求等文件(http://127.0.0.1/)
我假設我可以簡單地通過手工編輯index.html
文件指向從根本上正確的路徑解決這個問題,但這似乎像黑客而不是修復。
誰能解釋什麼怎麼回事,爲什麼:
<base href="/">
適用於大多數人,但不是我?