How do I rewrite URLs in Dotnet 5+

  • Page Owner: Not Set
  • Last Reviewed: 2022-09-15

I need to add slashes to all URLs how do I do that?


Answer

Use a RewriteOption in the startup file.

Note, the regex may need a capture group () if you use one, use $1 to invoke them in the replacement. See example below.

Startup.cs:

var options = new RewriteOptions();
options.AddRedirect(@"^(.*[^/])$", "$1/", 301); 

app.UseRewriter(options);