How to use Swagger as Welcome Page of IAppBuilder in WebAPI

后端 未结 12 1393
遥遥无期
遥遥无期 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:34

    For ASP.NET Core the following pull request was created: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/pull/486

    In the meantime the following workaround can be used:

    public static IApplicationBuilder UseSwaggerUI(
            this IApplicationBuilder app,
            Action setupAction)
        {
            var options = new SwaggerUIOptions();
            setupAction?.Invoke(options);
    
            // This method reads an internal property value 
            // http://dotnetfollower.com/wordpress/2012/12/c-how-to-set-or-get-value-of-a-private-or-internal-property-through-the-reflection/
            var indexSettings = options.GetPropertyValue("IndexSettings");
            // Serve swagger-ui assets with the FileServer middleware, using a custom FileProvider
            // to inject parameters into "index.html"
            var fileServerOptions = new FileServerOptions
            {
                RequestPath = string.IsNullOrWhiteSpace(options.RoutePrefix) ? string.Empty : $"/{options.RoutePrefix}",
                FileProvider = new SwaggerUIFileProvider(indexSettings.ToTemplateParameters()),
                EnableDefaultFiles = true,
                StaticFileOptions =
                {
                    ContentTypeProvider = new FileExtensionContentTypeProvider()
                }
            };
            app.UseFileServer(fileServerOptions);
    
            return app;
        }
    

    Cheers

提交回复
热议问题