【问题标题】:IIS URL Rewrite RulesIIS URL 重写规则
【发布时间】:2014-10-27 22:51:12
【问题描述】:

我有一个使用 URL 重写进行链接的 AngularJS 应用程序。我的重写规则如下:

<rewrite>
<rules> 
  <rule name="MainRule" 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_FILENAME}" matchType="Pattern" pattern="^api/(.*)" negate="false" />
    </conditions>
    <action type="Rewrite" url="Default.cshtml" />
  </rule>
</rules>

我在 SO:How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode? 上找到了这个解决方案,并进行了一项修改以允许我的 API 通过。

基本上,我想将我的所有请求重定向到Default.cshtml,除了对我的 Web API 的调用。当我在基本 URL 上加载应用程序时,这很有效,例如:localhost/myapp。但是,如果我在有角度的路线上重新加载页面,例如:localhost/myapp/dashboard,它会失败说:

<Error><Message>No HTTP resource was found that matches the request URI 'http://localhost/myapp/dashboard'.</Message>
<MessageDetail>No type was found that matches the controller named 'dashboard'.</MessageDetail></Error>

我有一个解决办法:

 <rewrite>
  <rules>
    <rule name="Default" stopProcessing="true">
      <match url="^(?!lib|api|dist|assets|app/|bower|common|main|signalr|templates|bower_components/).*" />
      <action type="Rewrite" url="Default.cshtml" />
    </rule>
  </rules>
</rewrite>

而且效果很好。有什么想法可以利用上述更清洁的解决方案并仍然完成重写吗?

【问题讨论】:

    标签: iis url-rewriting


    【解决方案1】:

    我需要更改模式并将{REQUEST_FILE} 更改为{REQUEST_URI} 在几个地方。在此处查看我的演示:

    <rewrite>
      <rules>
        <rule name="MainRule" 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}" matchType="Pattern" pattern="api/(.*)" negate="true" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="signalr/(.*)" negate="true" />
          </conditions>
          <action type="Rewrite" url="Default.cshtml" />
        </rule>
      </rules>
    </rewrite>
    

    【讨论】:

    • 那个信号模式是救命稻草。为此挣扎了几个小时。未来用户的另一点是,如果您在 startup.cs app.start() 中指定自己的根路径,您将遇到问题。我必须删除“/www”的根路径,然后将其全部留给 signalR 映射。
    • 我爱你,伙计!您的信号器模式是救命稻草² 只需将其更改为 Admin(我的文件夹应用程序)并像魅力一样工作。我不知道这是什么意思,因为我只是一个设计师 lmao,但它确实有效。所以谢谢你。
    猜你喜欢
    • 2014-05-01
    • 2011-04-21
    • 2014-01-05
    • 1970-01-01
    • 2012-06-19
    • 2020-01-05
    • 1970-01-01
    相关资源
    最近更新 更多