Angular SPA + WebApi的IIS URL重写规则

Pau*_*aul 4 iis-7 rewrite

我有一个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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Chr*_*ris 10

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

    <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>
Run Code Online (Sandbox Code Playgroud)

尝试这种方式,但如果您不希望这些请求进入服务器,请随时删除"| account | manage".

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


Pau*_*aul 0

更新这一行

<match url=".*" />
Run Code Online (Sandbox Code Playgroud)

对此

<match url=".*|.*/.*$" />
Run Code Online (Sandbox Code Playgroud)