SwaggerModule.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.Reflection;
  2. namespace WebTemplate.ServerAspects.Swagger;
  3. public class SwaggerModule : IAppConfigurationModule
  4. {
  5. public void ConfigureServices(IServiceCollection services, IConfigurationRoot config)
  6. {
  7. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  8. services.AddEndpointsApiExplorer();
  9. services.AddSwaggerGen(swaggerGen =>
  10. {
  11. swaggerGen.SupportNonNullableReferenceTypes();
  12. swaggerGen.EnableAnnotations();
  13. // The StringEnumSchemaFilter changes the representation of enums from numbers to strings in
  14. // the OpenApi document. This assumes that the JSON serializer has ben configured to use the
  15. // JsonStringEnumConverter - the two play hand-in-hand.
  16. swaggerGen.SchemaFilter<StringEnumSchemaFilter>();
  17. // This requires the <GenerateDocumentationFile> property to be set to true in the
  18. // .csrpoj file
  19. var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
  20. var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFilename);
  21. if (File.Exists(xmlPath))
  22. {
  23. swaggerGen.IncludeXmlComments(xmlPath);
  24. }
  25. });
  26. }
  27. public void ConfigureApplication(WebApplication app)
  28. {
  29. if (app.Environment.IsDevelopment())
  30. {
  31. app.UseSwagger();
  32. app.UseSwaggerUI();
  33. }
  34. }
  35. }