By default when you add Swagger (through SwashBuckle) in your ASP.NET Web API project, the swagger UI is available on following url: https://localhost:44300/swagger .
However because this project was only hosting this API, we wanted to show the Swagger UI on the root page. To get this working we added another route to our WebApiConfig.cs file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class WebApiConfig | |
{ | |
public static void Register(HttpConfiguration config) | |
{ | |
// Web API routes(should be invoked first to avoid errors) | |
config.MapHttpAttributeRoutes(); | |
config.Routes.MapHttpRoute( | |
name: "swagger", | |
routeTemplate: "", | |
defaults: null, | |
constraints: null, | |
handler: new Swashbuckle.Application.RedirectHandler((message => message.RequestUri.ToString()), "swagger")); | |
config.Routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: new { id = RouteParameter.Optional } | |
); | |
} | |
} |