irc-netcore-api/IRaCIS.Core.API/_ServiceExtensions/SwaggerSetup.cs

131 lines
4.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
using Swashbuckle.AspNetCore.SwaggerGen;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
namespace IRaCIS.Core.API;
public enum SwaggerVersion
{
[Description("文件记录(FileRecord)")]
FileRecord = -1,
[Description("医生模块(Reviewer)")]
Reviewer = 1,
[Description("项目模块(Trial)")]
Trial = 2,
[Description("入组模块(Enroll)")]
Enroll = 3,
[Description("工作量模块(Workload)")]
Workload = 4,
[Description("通用信息获取(Common)")]
Common = 5,
[Description("机构信息模块(Institution)")]
Institution = 6,
[Description("统计模块(DashboardStatistics)")]
DashboardStatistics = 7,
[Description("财务模块(Financial)")]
Financial = 8,
[Description("管理模块(Management)")]
Management =9,
[Description("影像模块(Image)")]
Image =10,
[Description("读片模块(Reading)")]
Reading =11
};
public static class SwaggerSetup
{
public static void AddSwaggerSetup(this IServiceCollection services)
{
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(options =>
{
typeof(SwaggerVersion).GetFields(BindingFlags.Public | BindingFlags.Static).ToList()
.ForEach(field =>
{
var description = field.GetCustomAttribute<DescriptionAttribute>()?.Description ?? field.Name;
options.SwaggerDoc(field.Name, new Microsoft.OpenApi.Models.OpenApiInfo
{
Version = field.Name,
Description = $"{field.Name} API",
Title = description // 使用Description作为Title
});
});
// 接口排序
options.OrderActionsBy(o => o.GroupName);
//添加注释
var basePath = AppContext.BaseDirectory;
var xmlPath1 = Path.Combine(basePath, "IRaCIS.Core.Application.xml");
var xmlPath2 = Path.Combine(basePath, "IRaCIS.Core.API.xml");
options.IncludeXmlComments(xmlPath1, true);
options.IncludeXmlComments(xmlPath2, true);
// 在header中添加token传递到后台
options.OperationFilter<SecurityRequirementsOperationFilter>();
// 添加登录按钮
options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme()
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
//In = "header",
//Type = "apiKey"
});
});
}
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
typeof(SwaggerVersion).GetFields(BindingFlags.Public | BindingFlags.Static).ToList()
.ForEach(field =>
{
var description = field.GetCustomAttribute<DescriptionAttribute>()?.Description ?? field.Name;
options.SwaggerEndpoint($"swagger/{field.Name}/swagger.json", $"{description}");
});
var data = Assembly.GetExecutingAssembly().Location;
options.IndexStream = () => Assembly.GetExecutingAssembly()
.GetManifestResourceStream("IRaCIS.Core.API.wwwroot.swagger.ui.Index.html");
//路径配置设置为空表示直接在根域名localhost:8001访问该文件,
//注意localhost:8001/swagger是访问不到的去launchSettings.json把launchUrl去掉如果你想换一个路径直接写名字即可比如直接写c.Route = "doc";
options.RoutePrefix = string.Empty;
//DocExpansion设置为none可折叠所有方法
options.DocExpansion(DocExpansion.None);
//DefaultModelsExpandDepth设置为 - 1 可不显示models
options.DefaultModelsExpandDepth(-1);
});
}
}