How to use Swagger as Welcome Page of IAppBuilder in WebAPI
In ASP.NET Core, you can simply just change the RoutePrefix when registering SwaggerUI to empty string.
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "";
...
};
No redirect configuration required, unless you still want /swagger
or something similar in the path.
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
For Asp.Net core use this:
app.Run(context => {
context.Response.Redirect("swagger/ui");
return Task.CompletedTask;
});
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.