using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.Filters; using Znyc.CloudCar.Swagger; namespace Znyc.CloudCar.Core.Config { /// /// swagger /// public static class SwaggerSetup { /// /// /// public static void AddSwaggerSetup(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } var apiName = "云车二手小程序API"; services.AddSwaggerGen(s => { //遍历出全部的版本,做文档信息展示 typeof(CustomApiVersion.ApiVersion).GetEnumNames().ToList().ForEach(version => { s.SwaggerDoc(version, new OpenApiInfo { Version = version, Title = $"{apiName} 接口文档", Description = $"{apiName} HTTP API " + version, Contact = new OpenApiContact { Name = apiName, Email = "faker@znyc.com", Url = new Uri("https://Znyc.com") }, }); s.OrderActionsBy(o => o.RelativePath); }); try { //生成API XML文档 var basePath = AppContext.BaseDirectory; var xmlPath = Path.Combine(basePath, "doc.xml"); s.IncludeXmlComments(xmlPath); } catch (Exception) { } // 开启加权小锁 s.OperationFilter(); s.OperationFilter(); // 在header中添加token,传递到后台 s.OperationFilter(); // 必须是 oauth2 s.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme { Description = "JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)\"", Name = "Authorization",//jwt默认的参数名称 In = ParameterLocation.Header,//jwt默认存放Authorization信息的位置(请求头中) Type = SecuritySchemeType.ApiKey }); }); } } }