How to use Swagger as Welcome Page of IAppBuilder in WebAPI

Philippe Matray picture Philippe Matray · May 4, 2015 · Viewed 31k times · Source

I try to use Swagger with Microsoft WebAPI 2.

For the moment, I've the following call in a method.

appBuilder
   .ConfigureOAuth()
   .UseWebApi(configuration)
   .UseWelcomePage();

If I want to use Swagger, I must use this url "https://localhost:44300/swagger" which one works very well.

I want my home page redirects to the url of my swagger, perhaps as follows but this sample doesn't works.

    appBuilder
       ...
       .UseWelcomePage("/swagger");

Any idea ?

Answer

patrickbadley picture patrickbadley · May 24, 2016

I got this working how I wanted by adding a route in RouteConfig.cs like so:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapHttpRoute(
            name: "swagger_root", 
            routeTemplate: "", 
            defaults: null, 
            constraints: null,
            handler: new RedirectHandler((message => message.RequestUri.ToString()), "swagger"));

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

See this code from swashbuckle to see what's going on: https://github.com/domaindrivendev/Swashbuckle/blob/master/Swashbuckle.Core/Application/RedirectHandler.cs