关于这个问题,很多问题都是关于这个的。我已经阅读了很多,他们中的大多数都指向配置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="/">
适用于大多数人,但不是我?