/// <summary> /// /// </summary> /// <param name="app"></param> static public void UseSwaggerUI(this IApplicationBuilder app) { // 读取配置信息 SwaggerDocInfo doc = SwaggerDocInfo.Load(); #region Swagger // Enable middleware to serve generated Swagger as a JSON endpoint app.UseSwagger(); // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.) app.UseSwaggerUI(options => { options.SwaggerEndpoint(doc.EndpointUrl, doc.EndpointDesc); // 启用地址路径 options.EnableDeepLinking(); // 打开页面时,默认折叠标签 options.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None); // 可以根据标签名称对接口进行过滤 options.EnableFilter(); //options.DefaultModelExpandDepth(3); //options.DefaultModelsExpandDepth(3); //options.ShowRequestHeaders(); }); #endregion }
/// <summary> /// /// </summary> /// <param name="services"></param> static public void SwaggerRegister(this IServiceCollection services) { //读取配置信息 SwaggerDocInfo doc = SwaggerDocInfo.Load(); #region Swagger // Inject an implementation of ISwaggerProvider with defaulted settings applied. services.AddSwaggerGen(options => { // 设置接口文档的基本信息 options.SwaggerDoc(doc.Version, new Swashbuckle.AspNetCore.Swagger.Info { Version = doc.Version, Title = doc.Title, Description = doc.Description, TermsOfService = doc.TermsOfService, Contact = new Swashbuckle.AspNetCore.Swagger.Contact { Name = doc.ContactName, Email = doc.ContactEmail, Url = doc.ContactUrl } }); // 启用便签功能,实现接口按指定分类进行分组 options.EnableAnnotations(); // 遍历所有的注释文件,并添加到接口文档中 CopyXmlFile(); // 拷贝引用程序集目录下的xml文件 foreach (var file in GetXmlFile()) { options.IncludeXmlComments(file); } // 增加默认的注释信息 options.DocumentFilter <AppendComment>(); // 设置接口的排序 options.OrderActionsBy(SwaggerOrderBy.Order); // 设置登录验证参数的效果 options.AddSecurityDefinition("oauth2", new ApiKeyScheme { Description = $"Token验证。格式:Bearer token \r\n\t 注意:Bearer与token之间需要一个空格连接", In = "header", Name = "Authorization", Type = "apiKey" }); options.OperationFilter <SecurityRequirementsOperationFilter>(); // 官方登录验证的处理 options.OperationFilter <AuthorizationParameterFilter>(); // 设置接口备注,显示权限名称 //options.OperationFilter<SwaggerDecryptFilter>(); // 设置参数加密上传的效果 }); #endregion }