我们正在处理类似于这些404 Refresh 场景的情况 我们处于 html 5 模式,因此我们需要重写以修复 url。问题是,适用于已部署应用程序的解决方案在开发期间无法在 Visual Studio 中运行。
我们针对已部署应用程序的解决方案通过在 web.config 中添加以下内容来工作:
<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" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/myAppName/" />
</rule>
</rules>
</rewrite>
然后对 index.html 进行以下更改:
<base href="/myAppName/">
除了我们在 Angular 中构建的链接之外,这大部分都很有效。例如
<a href="/partDetails/"></a>
部署的应用程序不会将“myAppName”放在 url 中。为什么是这样?
第二个问题是在我们的 Visual Studio 开发环境中<base href="/myAppName/">
破坏了应用程序。这会使应用程序在输出中生成无穷无尽的错误字符串,如下所示:
SourceMap http://localhost:40288/app.module.js.map read failed: Unexpected character encountered while parsing value: <. Path '', line 4, position 1..SourceMap http://localhost:40288/app.core.module.js.map read failed: Unexpected character encountered while parsing value: <. Path '', line 4, position 1..SourceMap http://localhost:40288/app.config.js.map read failed: Unexpected character encountered while parsing value: <. Path '', line 4, position 1..
Exception was thrown at line 1247, column 4 in eval code
0x800a139e - JavaScript runtime error: SyntaxError
Exception was thrown at line 1265, column 4 in eval code
0x800a139e - JavaScript runtime error: SyntaxError
这似乎是一个无限循环,直到内存耗尽。但是,如果我从中删除应用程序名称<base href="/myAppName/">
,它似乎工作正常,但随后在服务器上中断。
问题出在哪里的任何想法?为什么解决方案只能在一方面发挥作用,开发或部署,我们如何解决这个问题?