1

I've got swagger added to my Startup.cs file and it shows my api just fine except when someone changes the capitalization of the word "Help" in the url. If I put in the url like this: https://sitename/Help/index.html This takes me to my correct api documentation, but if I put in this: https://sitename/help/index.html This takes me to the default swagger petstore api

Can anyone help me out with this? The company I work for is requesting it to work the same with or without capitalizing "Help"

Michael Sheely
  • 961
  • 2
  • 10
  • 31
  • 1
    Could you paste the swagger options without the sensitive data, please? – Rafael Biz Jun 07 '21 at 14:43
  • 1
    Which swagger package are you using and which version? – Rafael Biz Jun 07 '21 at 14:44
  • You may need to set up a 301 redirect from lowercase `/help/*` to `/Help/*`. URLs are [case-sensitive](https://stackoverflow.com/a/7996997/113116) in general so technically `/help` and `/Help` are two different URLs. – Helen Jun 07 '21 at 14:55

1 Answers1

0

URL Rewrite Middleware can be used for this purpose. The middleware allows to modify request URLs based on predefined rules. The middleware is provided by the Microsoft.AspNetCore.Rewrite package, which is found in the ASP.NET Core applications by default.

URL rewrite rules can be established by creating chained instances of RewriteOptions and passing them into the URL Rewrite Middleware.

By adding the following code segment into Configure method of Startup.cs, the re-writing of url from /Help/index.html to /help/index.html can be achieved.

var options = new RewriteOptions()
            .AddRedirect("^Help/(.*)", "help/$1");
            
app.UseRewriter(options);

Here is how it looks after introducing the URL rewrite option:

Original request: /Help/index.html

enter image description here

Yared
  • 2,206
  • 1
  • 21
  • 30