IIS Url rewrite rule for Angular SPA + WebApi

I have a SPA + WebAPI application, they are both in the same project. I want a WebAPI application to handle requests from the "api" directory. This way, everything under the api will be processed by the server, everything else will be processed by the Angular SPA.

I currently have

   <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>

      

This works fine except when I go to the url

http://localhost/customers/2

(which downloads the document but all resources come with the wrong mime type which I ended up due to the redirect rules above)

URL http://localhost/customers

works fine, will be redirected by my SPA app

How do I get everything to redirect to my SPA except for requests that end up in the API directory?

+3


source to share


2 answers


I do this with two rules. One for whitelisting server controllers I want to go to asp.net which includes / api as well as authentication files under / Account and / Manage. The next one sends everything else to angular, unless it's a file or folder 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>

      



Try this, but feel free to remove "| account | manage" if you don't want these requests sent to the server.

To see our entire ruleset, check out this question I posted where I handle HTTPS and canonical hostnames.

+6


source


Update this line

<match url=".*" />

      



For this

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

      

0


source







All Articles