Ejemplo n.º 1
0
 private static OpenApiInfo GetInfo(SwaggerOptionsConfig swaggerConfig)
 {
     return(new OpenApiInfo
     {
         Version = "v1",
         Title = swaggerConfig.Title,
         Description = swaggerConfig.Description,
         //TermsOfService = new Uri("https://example.com/terms"),
         Contact = new OpenApiContact
         {
             Name = swaggerConfig.ContactName,
             Email = swaggerConfig.ContactEmail,
             Url = !string.IsNullOrWhiteSpace(swaggerConfig.ContactUrl)
                 ? new Uri(swaggerConfig.ContactUrl)
                 : null,
         },
         //License = new OpenApiLicense
         //{
         //    Name = "Use under LICX",
         //    Url = new Uri("https://example.com/license"),
         //}
     });
 }
        public static IApplicationBuilder ConfigSwagger(this IApplicationBuilder app, SwaggerOptionsConfig config,
                                                        Func <Stream> customPageLoader = null)
        {
            //https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-3.1&tabs=visual-studio
            // Enable middleware to serve generated Swagger as a JSON endpoint.

            app.UseSwagger(option =>
            {
                option.RouteTemplate = config.JsonRoute;

                option.PreSerializeFilters.Add((swaggerDoc, httpRequest) =>
                {
                    if (!httpRequest.Headers.ContainsKey("X-Forwarded-Host"))
                    {
                        return;
                    }

                    var serverUrl = $"{httpRequest.Headers["X-Forwarded-Proto"]}://" +
                                    $"{httpRequest.Headers["X-Forwarded-Host"]}/" +
                                    $"{httpRequest.Headers["X-Forwarded-Prefix"]}";

                    swaggerDoc.Servers = new List <OpenApiServer>()
                    {
                        new OpenApiServer {
                            Url = serverUrl
                        }
                    };
                });
            }
                           );

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.InjectStylesheet("css/customcss.css");

                if (customPageLoader != null)
                {
                    c.IndexStream = customPageLoader;
                }

                c.SwaggerEndpoint(config.UiEndpoint, config.DefinitionDescription);

                //To serve the Swagger UI at the app's root set the RoutePrefix property to an empty string
                c.RoutePrefix = string.Empty;

                c.EnableFilter();
                c.DisplayRequestDuration();
            });

            return(app);
        }