IIS中的AngularJS路由

AngularJS Routing in IIS

本文关键字:路由 AngularJS 中的 IIS      更新时间:2023-09-26

我开发了托管在IIS根目录中的angularJs应用程序,即我让它在本地主机中运行。如果我在浏览器中键入 localhost 并按回车键,我的应用程序将打开并运行良好。

问题是,我有一些其他应用程序同时在本地主机下运行。例如)本地主机/hris,本地主机/CMS等,这些都是 asp.net 应用程序。在我托管了角度应用程序后,如果尝试打开我的应用程序,它会在浏览器控制台中向我显示以下 javascript 错误......

Uncaught Error: ASP.NET Ajax client-side framework failed to load.
ScriptResource.axd:1 Uncaught SyntaxError: Unexpected token <

我多次收到这两个错误...

我该如何克服这个问题?

仅供参考:-我已经在我的角度应用程序中实现了路由。还有一件事是 URL 重写,我在我的 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" />
            </conditions>
            <action type="Rewrite" url="/" />
        </rule>
    </rules>
</rewrite>

我是 angularJS 的初学者。

有人帮我这个...

提前感谢...

基于 ASP.NET 的Web应用程序经常向WebResources.axd文件发出请求,以检索程序集资源并将其提供给Web浏览器。服务器上不存在此类文件,因为 ASP.NET 请求 WebResources.axd 时动态生成内容。因此,如果您有一个 URL 重写规则,该规则仅在请求的 URL 与 Web 服务器文件系统上的文件或文件夹不对应时才执行重写或重定向,则该规则可能会意外重写向 WebResources.axd 发出的请求,从而破坏您的应用程序。

如果向重写规则添加一个额外条件,则可以轻松防止此问题:

<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="ITA_CALENDER_API/(.*)" negate="true" />
            <add input="{URL}" negate="true" pattern="'.axd$" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>