Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Swagger as Welcome Page of IAppBuilder in WebAPI

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 ?

like image 794
Philippe Matray Avatar asked May 04 '15 11:05

Philippe Matray


People also ask

What is Swagger in Web API?

Swagger (OpenAPI) is a language-agnostic specification for describing REST APIs. It allows both computers and humans to understand the capabilities of a REST API without direct access to the source code. Its main goals are to: Minimize the amount of work needed to connect decoupled services.

Where is Swaggerconfig CS go?

After Installation, you can see the swaggerconfig. cs under app_start folder in your respective project. At minimum, we need this line to enable Swagger and Swagger UI.


2 Answers

In the Startup.cs file in the Configuration(IAppBuilder app) method I used this line of code to cause it to redirect on load to the swagger welcome page.

app.Run(async context => {      context.Response.Redirect("swagger/ui/index");  });  

So the full method I am using is as follows

[assembly: OwinStartup(typeof(AtlasAuthorizationServer.Startup))] namespace AtlasAuthorizationServer {     public partial class Startup     {         public void Configuration(IAppBuilder app)         {             ConfigureAuth(app);              HttpConfiguration config = new HttpConfiguration();             WebApiConfig.Register(config);             app.UseWebApi(config);              app.Run(async context => {                 context.Response.Redirect("swagger/ui/index");             });         }     } } 

Note that this is going to cause a green warning in visual studio. I am sure there is some way to mimic this as asynchronous with an await call in the function.

like image 26
Brad LaPratt Avatar answered Oct 03 '22 17:10

Brad LaPratt


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

like image 160
patrickbadley Avatar answered Oct 03 '22 16:10

patrickbadley