I have a WebApi project with Swashbuckle installed onto it.
In default setup, I must open in browser http://localhost:56131/swagger/ui/index
to view my operations description and test page. I want it to be accessible from root of the site: http://localhost:56131/
. How can I achieve this?
By default, Swagger UI is accessible at /q/swagger-ui .
Generating HTML documentation using Swagger-ui.Add Swagger-ui dependency - (https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui). This will provide an endpoint at the root url, which provides the Swagger HTML documentation. This will be located at localhost:8080/swagger-ui.
The swagger-config. yaml in the project root directory, if it exists, is baked into the application. configuration object passed as an argument to Swagger UI ( SwaggerUI({ ... }) )
Influenced by this answer to similar question, slightly modified code:
public class WebApiConfig
{
public static void Configure(IAppBuilder app)
{
var httpConfig = new HttpConfiguration();
// Attribute routing
config.MapHttpAttributeRoutes();
// Redirect root to Swagger UI
config.Routes.MapHttpRoute(
name: "Swagger UI",
routeTemplate: "",
defaults: null,
constraints: null,
handler: new RedirectHandler(SwaggerDocsConfig.DefaultRootUrlResolver, "swagger/ui/index"));
// Configure OWIN with this WebApi HttpConfiguration
app.UseWebApi(httpConfig);
}
}
This way it is not necessary to create new WebAPI controller as so did @bsoulier in his answer.
This solution is based on already existing class RedirectHandler
in Swashbuckle.Core
assembly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With