using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Core.Helper; using static WIDESEAWCS_Core.Extensions.CustomApiVersion; using Swashbuckle.AspNetCore.Filters; using Microsoft.Extensions.Logging; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; namespace WIDESEAWCS_Core.Extensions { /// <summary> /// Swagger /// </summary> public static class SwaggerSetup { /// <summary> /// Swagger /// </summary> /// <param name="services"></param> /// <exception cref="ArgumentNullException"></exception> public static void AddSwaggerSetup(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); var basePath = AppContext.BaseDirectory; //var basePath2 = Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath; var ApiName = AppSettings.app(new string[] { "ApiName" }); services.AddSwaggerGen(c => { //éåŽ†å‡ºå…¨éƒ¨çš„ç‰ˆæœ¬ï¼Œåšæ–‡æ¡£ä¿¡æ¯å±•示 typeof(ApiVersions).GetEnumNames().ToList().ForEach(version => { c.SwaggerDoc(version, new OpenApiInfo { Version = version, Title = $"{ApiName} æŽ¥å£æ–‡æ¡£â€”—{RuntimeInformation.FrameworkDescription}", Description = $"{ApiName} HTTP API " + version, }); c.OrderActionsBy(o => o.RelativePath); }); c.UseInlineDefinitionsForEnums(); try { //这个就是刚刚é…置的xml文件å //var xmlPath = Path.Combine(basePath, "WIDESEAWCS_Server.xml"); //é»˜è®¤çš„ç¬¬äºŒä¸ªå‚æ•°æ˜¯false,这个是controller的注释,记得修改 //c.IncludeXmlComments(xmlPath, true); //这个就是Model层的xml文件å //var xmlModelPath = Path.Combine(basePath, "WIDESEAWCS_Server.Model.xml"); //c.IncludeXmlComments(xmlModelPath); } catch (Exception ex) { //log.Error("Blog.Core.xmlå’ŒBlog.Core.Model.xml 丢失,请检查并拷è´ã€‚\n" + ex.Message); } // å¼€å¯åŠ æƒå°é” c.OperationFilter<AddResponseHeadersFilter>(); c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>(); // 在header䏿·»åŠ tokenï¼Œä¼ é€’åˆ°åŽå° c.OperationFilter<SecurityRequirementsOperationFilter>(); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "JWT授æƒtokenå‰é¢éœ€è¦åŠ ä¸Šå—æ®µBearerä¸Žä¸€ä¸ªç©ºæ ¼,如Bearer token", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, BearerFormat = "JWT", Scheme = "Bearer" }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, new string[] { } } }); }).AddControllers() .ConfigureApiBehaviorOptions(options => { options.SuppressConsumesConstraintForFormFileParameters = true; options.SuppressInferBindingSourcesForParameters = true; options.SuppressModelStateInvalidFilter = true; options.SuppressMapClientErrors = true; options.ClientErrorMapping[404].Link = "https://*/404"; }); //services.AddSwaggerGenNewtonsoftSupport(); } } /// <summary> /// 自定义版本 /// </summary> public class CustomApiVersion { /// <summary> /// Api接å£ç‰ˆæœ¬ 自定义 /// </summary> public enum ApiVersions { /// <summary> /// V1 版本 /// </summary> V1 = 1, /// <summary> /// V2 版本 /// </summary> V2 = 2, } } }