Vincent Escarcha
Vincent Escarcha

Reputation: 21

How to run AngularJs html5mode on ASP.Net Core using URL Rewrite?

I have an angularjs app running on .net framework. when i try to run it on .net core, i cannot fix the html5mode fallback on refreshing the page gets error 404

I already tried to create a web.config for url rewrite and creating a middleware.

   <system.webServer>
    <rewrite>
      <rules>
        <rule name="AngularJS Routes" 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>
  </system.webServer>

I want to run angularjs html5mode on .net core using URL Rewrite

Upvotes: 1

Views: 256

Answers (1)

Vincent Escarcha
Vincent Escarcha

Reputation: 21

On your project, create a xml file like IISUrlRewrite.xml and copy paste the configuration

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

Then on your Startup.cs Configure() read that file and attach it on RewriteOptions().AddIISUrlRewrite

   using (StreamReader iisUrlRewriteReader = File.OpenText("IISUrlRewrite.xml"))
   {
            var rules = new RewriteOptions();

            rules.AddIISUrlRewrite(iisUrlRewriteReader);

            app.UseRewriter(rules);
   }
   app.UseFileServer();

source reference here

Upvotes: 1

Related Questions