title: Web.config file IIS rules for URL rewrites for Angular hosted inside ASP.NET Core not working as expected
body: <p>I am trying to get the IIS web.config rule rewrites to work but I have been unsuccessful thus far. Based on my research I need a single web.config file at the root of the main project.</p>
  
  <p><a href="https://i.stack.imgur.com/eh85m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eh85m.png" alt="enter image description here"></a></p>
  
  <p>The contents of this web.config file are as follow:</p>
  
  <pre><code>`&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;location path="." inheritInChildApplications="false"&gt;
      &lt;system.webServer&gt;
        &lt;rewrite&gt;
          &lt;rules&gt;
            &lt;rule name="SpaRewriteRule" stopProcessing="true"&gt;
              &lt;match url=".*"/&gt;
              &lt;conditions logicalGrouping="MatchAll"&gt;
                &lt;add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/&gt;
                &lt;add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/&gt;
                &lt;add input="{REQUEST_URI}" pattern="^/(api)" negate="true"/&gt;
              &lt;/conditions&gt;
              &lt;action type="Rewrite" url="index.html"/&gt;
            &lt;/rule&gt;
          &lt;/rules&gt;
        &lt;/rewrite&gt;
        &lt;staticContent&gt;
          &lt;mimeMap fileExtension=".*" mimeType="application/octet-stream" /&gt;
        &lt;/staticContent&gt;
        &lt;handlers&gt;
          &lt;add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /&gt;
        &lt;/handlers&gt;
        &lt;aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess"&gt;
          &lt;environmentVariables&gt;
            &lt;environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /&gt;
            &lt;environmentVariable name="COMPLUS_ForceENC" value="1" /&gt;
          &lt;/environmentVariables&gt;
        &lt;/aspNetCore&gt;
      &lt;/system.webServer&gt;
    &lt;/location&gt;
  &lt;/configuration&gt;`
  </code></pre>
  
  <p>I have already installed the IIS Rewrite module. Based on this set up my expectation is that:</p>
  
  <ol>
  <li>Once angular app is initially loaded, I would be able to refresh the page successfully - this is not the case</li>
  </ol>
  
  <p>I have found a work around for this to add the following code in Startup.cs:</p>
  
  <pre><code>.Use(async (context, next) =&gt;
                  {
                      await next();
                      if (context.Response.StatusCode == 404 &amp;&amp; !Path.HasExtension(context.Request.Path.Value) &amp;&amp; !context.Request.Path.Value.StartsWith("/api/"))
                      {
                          context.Request.Path = "/index.html";
                          await next();
                      }
                  });
  </code></pre>
  
  <p>This however feels hacky, unless this is the accepted way of doing it now?</p>
  
  <p>What is the right way of handling this? </p>
  
  <p>Thanks</p>