javascript - 服务器上的 Angular 链接中断

标签 javascript angularjs iis visual-studio-2015 html5mode

我们正在处理类似的情况 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/

相关文章:

javascript - 动态模型操纵

jquery - 指令宽度(以像素为单位)

javascript - 如何加载 MVC 局部 View 并保持 Angular JS 正常工作

css - 获取 twig 模板以查看 css 文件时遇到问题

angular - 在 IIS 上设置 Angular 深度链接

javascript - 经过另一个时隐藏固定元素

javascript - 使用按钮检查 html 复选框

javascript - 为什么FOR循环只迭代一次?

javascript - 如果在具有 z 索引的固定元素外部单击,Angular Material md-menu 元素不会关闭

CSS在IIS6.0中不适用,但在WAMP测试环境中有效