HuanMengAdmin/admin-server/MiaoYu.Core.Swagger/CoreSwaggerBaseStartup.cs
2024-07-19 02:05:38 +08:00

54 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace MiaoYu.Core.Swagger;
public class CoreSwaggerBaseStartup : StartupModule<CoreSwaggerBaseStartup>
{
/// <summary>
/// 服务配置
/// </summary>
/// <param name="webApplicationBuilder"></param>
public override void ConfigureServices(WebApplicationBuilder webApplicationBuilder)
{
var services = webApplicationBuilder.Services;
var appName = Tools.GetAppName(webApplicationBuilder.Environment.ApplicationName);
var namespacePrefix = Tools.GetNamespacePrefix(webApplicationBuilder.Environment.ApplicationName);
// Swagger 注册Swagger生成器定义一个和多个Swagger 文档
webApplicationBuilder.Services
.AddSwaggerGen(options =>
{
options.CustomSchemaIds(type => type.FullName);
options.SwaggerDoc(appName, new OpenApiInfo
{
Title = appName
});
//为 Swagger JSON and UI设置xml文档注释路径
Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory)
.Where(w => w.EndsWith(".xml") && w.Contains(namespacePrefix))
.Select(w => w)
.ToList()
.ForEach(w => options.IncludeXmlComments(w, true))
;
});
}
/// <summary>
/// 使用服务
/// </summary>
/// <param name="webApplication"></param>
public override void Configure(WebApplication webApplication)
{
var appName = Tools.GetAppName(webApplication.Environment.ApplicationName);
// 启用Swagger中间件
webApplication.UseSwagger();
// 配置SwaggerUI
webApplication.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"{appName}/swagger.json", appName);
c.RoutePrefix = "swagger";
});
}
}