【问题标题】:IIS Url rewrite rule for Angular SPA + WebApiAngular SPA + WebApi 的 IIS Url 重写规则
【发布时间】:2015-02-09 10:16:46
【问题描述】:

我有一个 SPA + WebAPI 应用程序,它们都在同一个项目中。我只希望 WebAPI 应用程序处理来自目录“api”的请求。所以 api 下的任何东西都将由服务器处理,其他一切都将由 Angular SPA 处理。

我现在有

   <rewrite>
      <rules>
        <rule name="AngularJS" 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>

这工作正常,除非我转到 URL

http://localhost/customers/2(它会加载文档,但由于上面的重定向规则,所有资源都以错误的 mime 类型输入)

URL http://localhost/customers 工作正常将由我的 SPA 应用程序路由

除了来自 API 目录下的请求之外,如何将所有内容重定向到我的 SPA?

【问题讨论】:

  • 您找到解决此问题的方法了吗?遇到完全相同的困境

标签: iis-7 rewrite


【解决方案1】:

我这样做有两个规则。一个将我想要访问 asp.net 的服务器端控制器“列入白名单”,其中包括 /api 以及 /Account 和 /Manage 下的身份验证内容。接下来将所有其他内容发送到 Angular,除非它是文件或文件夹,就像您已经拥有的一样。

    <rule name="API Rule" stopProcessing="true">
      <match url="^(api|account|manage)(.*)$" />
      <action type="None" />
    </rule>
    <rule name="Angular 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>

尝试这种方式,但如果您不希望这些请求发送到服务器,请随意删除“|account|manage”。

要查看我的整个规则集,请查看我发布的 this question,我也在其中处理 HTTPS 和规范主机名。

【讨论】:

  • 这完美!感谢您发布您的解决方案!
【解决方案2】:

更新这一行

<match url=".*" />

到这里

<match url=".*|.*/.*$" />

【讨论】:

  • 这不起作用.. API 路由仍然有效,但 SPA 路由没有触发
猜你喜欢
  • 1970-01-01
  • 2014-10-27
  • 2014-05-01
  • 1970-01-01
  • 2011-04-21
  • 1970-01-01
  • 2014-01-05
相关资源
最近更新 更多