Roma
Roma

Reputation: 23

URL rewriting for static files in ASP.NET core 3 using Microsoft.AspNetCore.Rewrite extension

I have an issue with URL rewriting in asp.net core 3 using static files. So I want to rid of .html extension in URL.

My launchSettings.json looks like this:

"profiles": {
"IIS Express": {
  "commandName": "IISExpress",
  "launchBrowser": true,
  "launchUrl": "DemoSignUp.html",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
},

So to rewrite it to "DemoSignUp" I added these lines of code in my Startup.cs:

        app.UseRewriter(new RewriteOptions()
            .AddRedirect("DemoSignUp.html", "DemoSignUp")
            .AddRewrite("DemoSignUp", "DemoSignUp.html", skipRemainingRules: false));

But my URL still have .html extension

https://localhost:44319/DemoSignUp.html

Upvotes: 2

Views: 2084

Answers (1)

Ryan
Ryan

Reputation: 20116

You need to place the rewriter middleware before the static files middleware like:

app.UseRewriter(new RewriteOptions()
            .AddRedirect("DemoSignUp.html", "DemoSignUp")
            .AddRewrite("DemoSignUp", "DemoSignUp.html", skipRemainingRules: false));
app.UseStaticFiles();//after above rewriter rules

app.UseRouting();

//other middlewares     

Upvotes: 3

Related Questions