Angular SPA + WebApi的IIS URL重写规则 [英] IIS Url rewrite rule for Angular SPA + WebApi

查看:513
本文介绍了Angular SPA + WebApi的IIS URL重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a SPA + WebAPI application, they are both in the same project. I only want the WebAPI application to process requests from the directory "api". So anything under api will be handled by the server, everything else will be handled by the 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>

除非我转到网址

http:// localhost / customers / 2 (加载文档,但所有资源都以错误的mime类型进入,因为重定向,我已经得出结论以上规则)

http://localhost/customers/2 (which loads the doc but all resources are coming in with the wrong mime type I have concluded its because of the redirect rules above)

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

the URL http://localhost/customers works fine will be routed by my SPA app

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

How do I get everything to be redirected to my SPA except for the requests that come under the API directory?

推荐答案

我这样做有两个规则。一个白名单我要去asp.net的服务器端控制器,其中包括/ api以及/ Account和/ Manage下的身份验证内容。下一个将其他所有内容发送到角度,除非它是一个文件或文件夹,就像你已经拥有的那样。

I do this with two rules. One to "whitelist" the server-side controllers I want to go to asp.net, which includes /api but also the authentication stuff under /Account and /Manage. The next sends everything else to angular unless it's a file or folder, much like what you already have.

    <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希望这些请求转到服务器。

Try it this way, but feel free to remove "|account|manage" if you don't want those requests going to the server.

要查看我的整个规则集,请查看这个问题我发布了,我也处理了HTTPS和规范主机名。

To see my entire ruleset, check out this question I have posted, where I handle HTTPS and canonical hostnames as well.

这篇关于Angular SPA + WebApi的IIS URL重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆