我们正在处理类似的情况 404 Refresh scenarios 我们处于 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”放入网址中。这是为什么?
第二个问题是,在我们的 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/">
中删除应用程序名称,它似乎工作正常,但随后在服务器上崩溃了。
您知道问题出在哪里吗?为什么解决方案只能在开发或部署的一侧起作用?我们如何解决这个问题?
最佳答案
如果您为 <base>
创建绝对 URL指令,它应该可以工作。
例如,使用 asp:Literal
<head>
中名为“baseLink”部分:
Option Infer On
Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
Dim baseUrl = Request.Url.GetLeftPart(UriPartial.Authority) & VirtualPathUtility.ToAbsolute("~/") & "myAppName/"
baseLink.Text = $"<base href=""{baseUrl}"">"
End Sub
(我只是在末尾添加了 & "myAppName/"
- 我不在电脑前,无法确认是否应该使用 ....ToAbsolute("~/myAppName/")
来代替。)
关于javascript - 服务器上的 Angular 链接中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42375686/