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 ?
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.
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.
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.
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
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