|
| 1 | +using Microsoft.Extensions.Configuration; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | +using System.Reflection; |
| 6 | +using Microsoft.AspNetCore.Builder; |
| 7 | + |
| 8 | +namespace FacwareBase.Api.Extensions.Swagger |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Swagger extension |
| 12 | + /// </summary> |
| 13 | + public static class SwaggerExtension |
| 14 | + { |
| 15 | + #region Swagger Configuration |
| 16 | + /// <summary> |
| 17 | + /// Method to configure the Swagger Services within the Application services interface |
| 18 | + /// </summary> |
| 19 | + /// <param name="services">The Service Collection <see cref="IServiceCollection"/></param> |
| 20 | + /// <param name="config">The Service Collection <see cref="IConfiguration"/></param> |
| 21 | + public static void ConfigureSwaggerExtension(this IServiceCollection services, IConfiguration config) |
| 22 | + { |
| 23 | + services.AddSwaggerGen(c => |
| 24 | + { |
| 25 | + c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo |
| 26 | + { |
| 27 | + Title = config["SwaggerConfiguration:Title"], |
| 28 | + Version = config["SwaggerConfiguration:Version"], |
| 29 | + Description = config["SwaggerConfiguration:Description"] |
| 30 | + }); |
| 31 | + |
| 32 | + // Set the comments path for the Swagger JSON and UI. |
| 33 | + var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; |
| 34 | + var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); |
| 35 | + |
| 36 | + c.IncludeXmlComments(xmlPath); |
| 37 | + }); |
| 38 | + } |
| 39 | + #endregion |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Enable Swagger pipeline |
| 43 | + /// </summary> |
| 44 | + /// <param name="app">application configuration <see cref="IApplicationBuilder"/></param> |
| 45 | + /// <param name="config">application settings <see cref="IConfiguration"/></param> |
| 46 | + public static void EnableSwaggerPipeline(this IApplicationBuilder app, IConfiguration config) |
| 47 | + { |
| 48 | + app.UseSwagger(); |
| 49 | + app.UseSwaggerUI(option => |
| 50 | + { |
| 51 | + option.SwaggerEndpoint( |
| 52 | + config["SwaggerConfiguration:SwaggerJSONEndpoints"], |
| 53 | + $"{config["SwaggerConfiguration:Title"]} {config["SwaggerConfiguration:Version"]}"); |
| 54 | + // To serve the Swagger UI at the apps root |
| 55 | + option.RoutePrefix = string.Empty; |
| 56 | + }); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments