You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
4.3 KiB
C#

12 months ago
using DS.Module.Core;
using DS.Module.Core.Extensions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;
namespace DS.Module.Swagger;
/// <summary>
/// Swagger 服务
/// </summary>
public static class SwaggerInstall
{
public static IServiceCollection AddSwaggerInstall(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
10 months ago
string url = AppSetting.app(new string[] { "SwaggerDoc", "ContactUrl" });
12 months ago
var env = services.GetService<IWebHostEnvironment>();
var basePath = AppContext.BaseDirectory;
Console.WriteLine("基本路径" + basePath);
services.AddMvcCore().AddApiExplorer();
services.AddSwaggerGen(options =>
{
10 months ago
string contactName = AppSetting.app(new string[] { "SwaggerDoc", "ContactName" });
string contactNameEmail = AppSetting.app(new string[] { "SwaggerDoc", "ContactEmail" });
string contactUrl = AppSetting.app(new string[] { "SwaggerDoc", "ContactUrl" });
12 months ago
options.SwaggerDoc(contactName, new OpenApiInfo
{
10 months ago
Version = AppSetting.app(new string[] { "SwaggerDoc", "Version" }),
Title = AppSetting.app(new string[] { "SwaggerDoc", "Title" }),
Description = AppSetting.app(new string[] { "SwaggerDoc", "Description" }),
12 months ago
Contact = new OpenApiContact
10 months ago
{ Name = contactName, Email = contactNameEmail, Url = new Uri(contactUrl) },
12 months ago
License = new OpenApiLicense { Name = contactName, Url = new Uri(contactUrl) }
});
try
{
//就是这里
var xmlPath = Path.Combine(basePath, "Api.xml"); //这个就是刚刚配置的xml文件名
options.IncludeXmlComments(xmlPath, true); //默认的第二个参数是false这个是controller的注释记得修改
var xmlModelPath = Path.Combine(basePath, "Data.xml"); //这个就是Data层的xml文件名
options.IncludeXmlComments(xmlModelPath);
}
catch (Exception ex)
{
throw new Exception("Api.xml和Data.xml 丢失,请检查并拷贝。\n" + ex.Message);
}
options.CustomOperationIds(api =>
{
return api.TryGetMethodInfo(out MethodInfo methodInfo) ? methodInfo.Name : null;
});
// 一定要返回true
options.DocInclusionPredicate((docName, description) => { return true; });
// 开启加权小锁
options.OperationFilter<AddResponseHeadersFilter>();
options.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
// 在header中添加token传递到后台
options.OperationFilter<SecurityRequirementsOperationFilter>();
// 必须是 Bearer
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Description = "JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)\"",
Name = "Authorization", //jwt默认的参数名称
In = ParameterLocation.Header, //jwt默认存放Authorization信息的位置(请求头中)
Type = SecuritySchemeType.ApiKey,
// BearerFormat = "JWT", //标识承载令牌的格式 该信息主要是出于文档目的
Scheme = "Bearer" //授权中要使用的HTTP授权方案的名称
});
//在Heder中添加Token 传递到后台
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "oauth2"
}
},
new string[] { "readAccess", "writeAccess" }
}
});
});
return services;
}
}