48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
|
using Microsoft.AspNetCore.Builder;
|
|||
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|||
|
using Microsoft.Extensions.DependencyInjection;
|
|||
|
using Microsoft.Extensions.Options;
|
|||
|
using Mirea.Api.Endpoint.Configuration.Swagger;
|
|||
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|||
|
using System;
|
|||
|
using System.IO;
|
|||
|
|
|||
|
namespace Mirea.Api.Endpoint.Configuration.AppConfig;
|
|||
|
|
|||
|
public static class SwaggerConfiguration
|
|||
|
{
|
|||
|
public static IServiceCollection AddCustomSwagger(this IServiceCollection services)
|
|||
|
{
|
|||
|
services.AddSwaggerGen(options =>
|
|||
|
{
|
|||
|
options.SchemaFilter<SwaggerExampleFilter>();
|
|||
|
options.OperationFilter<SwaggerDefaultValues>();
|
|||
|
var basePath = AppDomain.CurrentDomain.BaseDirectory;
|
|||
|
|
|||
|
options.IncludeXmlComments(Path.Combine(basePath, "docs.xml"));
|
|||
|
options.IncludeXmlComments(Path.Combine(basePath, "ApiDtoDocs.xml"));
|
|||
|
});
|
|||
|
|
|||
|
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
|
|||
|
|
|||
|
return services;
|
|||
|
}
|
|||
|
|
|||
|
public static IApplicationBuilder UseCustomSwagger(this IApplicationBuilder app, IServiceProvider services)
|
|||
|
{
|
|||
|
app.UseSwagger();
|
|||
|
app.UseSwaggerUI(options =>
|
|||
|
{
|
|||
|
var provider = services.GetService<IApiVersionDescriptionProvider>();
|
|||
|
|
|||
|
foreach (var description in provider!.ApiVersionDescriptions)
|
|||
|
{
|
|||
|
var url = $"/swagger/{description.GroupName}/swagger.json";
|
|||
|
var name = description.GroupName.ToUpperInvariant();
|
|||
|
options.SwaggerEndpoint(url, name);
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
return app;
|
|||
|
}
|
|||
|
}
|