How to use Swagger as Welcome Page of IAppBuilder in WebAPI

后端 未结 12 1395
遥遥无期
遥遥无期 2020-12-24 05:59

I try to use Swagger with Microsoft WebAPI 2.

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

appBuilder
   .ConfigureOAuth()
   .UseWebApi(con         


        
相关标签:
12条回答
  • 2020-12-24 06:48

    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.

    0 讨论(0)
  • 2020-12-24 06:56

    I had similar problem and I solved it by customizing SwaggerUI url. This is my Configuration method:

    public void Configuration(IAppBuilder app)
    {
        var thisAssembly = typeof (Startup).Assembly;
    
        HttpConfiguration httpConfig = new HttpConfiguration();
    
        app.MapHttpAttributeRoutes();
        app.UseCors(CorsOptions.AllowAll);
        app.UseWebApi(httpConfig);
    
        httpConfig
            .EnableSwagger("api/{apiVersion}",c =>
            {
                c.IncludeXmlComments(string.Format(@"{0}\bin\Docs.xml", AppDomain.CurrentDomain.BaseDirectory));
                c.SingleApiVersion("v1", "My API");
            })
            .EnableSwaggerUi("{*assetPath}",c =>
            {
                c.CustomAsset("index", thisAssembly, "AspNetIdentity.WebApi.DocsAssets.index.html");
            });
    
        httpConfig.Routes.First(x => x.RouteTemplate == "{*assetPath}").Defaults["assetPath"] = "index";
    }
    

    This way when You go to localhost:44300 You'll get Swagger UI as startup page.

    0 讨论(0)
  • In .Net Core, just open Properties of the application, go to Debug tab, and write Swagger in the "Launch browser" text box,

    launch browser

    0 讨论(0)
  • 2020-12-24 06:59

    What you can do, just set Home Controller & Index Action as your Default, and modify your controller action as below:

    public class HomeController : Controller
    {
        // GET: /<controller>/
        public IActionResult Index()
        {
            return new RedirectResult("~/swagger");
        }
    }
    

    Short and quick solution to this problem.

    0 讨论(0)
  • 2020-12-24 07:00

    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

    0 讨论(0)
  • 2020-12-24 07:00

    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.

    0 讨论(0)
提交回复
热议问题