【问题标题】:IIS Rewrite loses path in Angular application upon refreshIIS 重写在刷新时在 Angular 应用程序中丢失路径
【发布时间】:2016-01-14 17:15:01
【问题描述】:

所以我一直在为我的 Angular.js 应用程序和 IIS 重写而苦苦挣扎。我的网站位于http://server/jwb/ 文件夹中。我让它重写到 /jwb/ 中的 index.html 文件,并且我有一个 index.html 的内部。问题是当我导航到http://server/jwb/parties/12345 时,通过角度完成时看起来很好,但是当我在浏览器上点击刷新时,我丢失了 CSS 和 javascript。我查看控制台,它们被重写为http://server/jwb/parties/js/nameoffile.js 等。我想要做的是告诉 IIS 重写始终查看它们真正驻留的 js、css 和 img 文件,它们位于http://server/jwb/jshttp://server/jwb/csshttp://server/jwb/img等。

将为我完成此任务的重写规则是什么?有没有比使用重写规则更好的方法?

我目前在我的 web.config 中有这个:

<rules>
    <rule name="Imported Rule 1" stopProcessing="true">
        <match url="^index\.html" ignoreCase="false" />
        <action type="None" />
    </rule>
    <rule name="Imported Rule 2" stopProcessing="true">
        <match url="." ignoreCase="false" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="index.html" />
    </rule>
</rules>

但是,这仍然会做一些奇怪的事情。我什至用相对路径做了一些测试,这就是我发现的:

我们现在已经测试了以下引用文件的方法:

<link rel="stylesheet" href="css/bootstrap.css" />

<link rel="stylesheet" href="/css/bootstrap.css" />

<link rel="stylesheet" href="./css/bootstrap.css" />

<link rel="stylesheet" href="../css/bootstrap.css" />

而且它们都不起作用。下面显示了当我们使用每个文件来引用我们的应用程序的路径时会发生什么。为了论证,我们的路径如下:

index --> http://10.34.34.46/jbvdev/calendar
documents --> http://10.34.34.46/jbvdev/documents/BC498484
caseNotes --> http://10.34.34.46/jbvdev/casenotes/CV/LA%20/2081/BC498484

【问题讨论】:

  • 尝试添加基本标签或使用页面本身资源的绝对路径
  • &lt;link rel="stylesheet" href="/css/bootstrap.css" /&gt; 实际上是一个绝对路径...在站点根目录中寻找 css 目录。前导 / 告诉浏览器从根目录或任何定义为根目录的基本标记开始

标签: javascript css angularjs iis


【解决方案1】:

我遇到了完全相同的问题...在完成所有添加基本引用和其他地方建议的内容之后仍然存在完全相同的问题。

所以,是的,请在 index.html 或任何作为 Angular 应用程序基础的页面中添加基本引用“/”。然后你需要恢复到好的 ol' 正则表达式......

我玩了一段时间是为了在正确的地方找到正确的位,并捕捉正确的部分......所以你需要为你的......

对我来说,我的道路是

  1. /Content/blah.whatever 用于 html 文件和 css 和
  2. /Scripts/some.js 等用于我的 javascript

发生的事情是 ON REFRESH ONLY 应用程序路由在 /Content 或 /Scripts 之前被插入...下面是最终为我修复它的一组规则...

<rewrite>
  <rules>
    <rule name="Removevesseldetailsscripts" stopProcessing="true">
      <match url="^(.*[/])Scripts/(.*)$" />
      <conditions>
      </conditions>
      <action type="Redirect" url="Scripts/{R:2}" />
    </rule>
    <rule name="Removevesseldetailscontent" stopProcessing="true">
      <match url="^(.*[/])Content/(.*)$" />
      <conditions>
      </conditions>
      <action type="Redirect" url="Content/{R:2}" />
    </rule>
    <rule name="Main Rule" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      </conditions>
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>

R:2 位是将角度路径放回 url 中正确的位置,显然还有 R:1 捕获了实际路径的根...

玩一玩,如果我能提供更多帮助,请告诉我!祝你好运!!!

【讨论】:

  • 这很完美!我不确定所有这些其他的重写规则,但这似乎是防弹的!任何使用它的人,请务必将操作规则指向您的应用程序真正所在的位置以及 index.html(或者您称之为主页的任何内容)。
猜你喜欢
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 2017-04-15
  • 2012-08-10
相关资源
最近更新 更多