コード例 #1
0
        public static void UseSwagger(this IApplicationBuilder app, ISwaggerConfiguration swaggerConfig)
        {
            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint(swaggerConfig.EndpointUrl, swaggerConfig.EndpointName);
            });
        }
コード例 #2
0
 public static void AddSwagger(this IServiceCollection services, ISwaggerConfiguration swaggerConfig)
 {
     services.AddSwaggerGen(c =>
     {
         c.SwaggerDoc(
             swaggerConfig.Version,
             new Info
         {
             Title   = swaggerConfig.DocTitle,
             Version = swaggerConfig.Version
         });
     });
 }
コード例 #3
0
        private static void AddJwtAuthentication(SwaggerGenOptions c, ISwaggerConfiguration swaggerConfiguration)
        {
            if (!swaggerConfiguration.JWTAuthentication)
            {
                return;
            }

            c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
            {
                Description  = "JWT Authorization header using the Bearer scheme (Example: 'Bearer 12345abcdef')",
                Name         = "Authorization",
                In           = ParameterLocation.Header,
                Type         = SecuritySchemeType.ApiKey,
                BearerFormat = "JWT",
                Scheme       = "Bearer"
            });

            c.OperationFilter <JWTSwaggerSecurityFilter>();
        }
コード例 #4
0
        public static void UseSwaggerSettings(this IApplicationBuilder app, ISwaggerConfiguration swaggerConfiguration)
        {
            if (!swaggerConfiguration.IsEnabled())
            {
                return;
            }

            app.UseSwagger(options => { options.RouteTemplate = swaggerConfiguration.JsonRoute; });
            app.UseSwaggerUI(opt =>
            {
                opt.SwaggerEndpoint(
                    swaggerConfiguration.UIEndpoint,
                    swaggerConfiguration.Description
                    );
                opt.DocumentTitle = swaggerConfiguration.Title;
                opt.DefaultModelsExpandDepth(0);
                opt.RoutePrefix = swaggerConfiguration.RoutePrefix;
            });
        }
コード例 #5
0
        public static void AddSwaggerSettings(this IServiceCollection services, ISwaggerConfiguration swaggerConfiguration)
        {
            if (!swaggerConfiguration.IsEnabled())
            {
                return;
            }

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc(swaggerConfiguration.Version, new OpenApiInfo
                {
                    Version     = swaggerConfiguration.Version,
                    Title       = swaggerConfiguration.Title,
                    Description = swaggerConfiguration.Description
                });

                AddJwtAuthentication(c, swaggerConfiguration);

                var xmlFile = $"{Assembly.GetEntryAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }
コード例 #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class.
 /// </summary>
 /// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param>
 /// <param name="config">Swagger Configuration from the Settings</param>
 public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider, ISwaggerConfiguration config)
 {
     _provider = provider;
     _config   = config;
 }
コード例 #7
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// <summary>
        /// Configures the application using the provided builder, hosting environment, and API version description provider.
        /// </summary>
        /// <param name="app">The current application builder.</param>
        /// <param name="env">The current environment</param>
        /// <param name="provider">The API version descriptor provider used to enumerate defined API versions.</param>
        /// <param name="swaggerConfig">Swagger Config in Settings (Swagger Plugin)</param>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider provider, ISwaggerConfiguration swaggerConfig)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            //From Swagger Plugin
            app.AddUseSwagger(provider, swaggerConfig);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
コード例 #8
0
        public static IApplicationBuilder AddUseSwagger(this IApplicationBuilder app, IApiVersionDescriptionProvider provider, ISwaggerConfiguration swaggerConfig)
        {
            app.UseSwagger(options =>
            {
                options.RouteTemplate = swaggerConfig.JsonRoute;
            });

            app.UseSwaggerUI(options =>
            {
                // build a swagger endpoint for each discovered API version
                foreach (var description in provider.ApiVersionDescriptions)
                {
                    options.DocumentTitle = swaggerConfig.PageTitle;
                    options.RoutePrefix   = swaggerConfig.RoutePrefix;
                    options.SwaggerEndpoint($"/{swaggerConfig.RoutePrefix}/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
                }
            });

            return(app);
        }