Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect from root url to /swagger/ui/index?

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?

like image 484
v.karbovnichy Avatar asked Jun 09 '16 11:06

v.karbovnichy


People also ask

What is the default swagger UI URL?

By default, Swagger UI is accessible at /q/swagger-ui .

How do I get swagger UI in HTML?

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.

Where do I put swagger config Yaml?

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({ ... }) )


1 Answers

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.

like image 87
v.karbovnichy Avatar answered Oct 08 '22 11:10

v.karbovnichy