netcore swagger 隐藏接口文档字段
一、添加操作筛选器
①、安装Swagger相关包
1、程序集包管理器控制台安装。
Install-Package Swashbuckle.AspNetCore.Swagger Install-Package Swashbuckle.AspNetCore.SwaggerGen Install-Package Swashbuckle.AspNetCore.SwaggerUI
2、Nuget程序包安装

②、添加操作筛选器
public class SwaggerOperationFilter : IOperationFilter { /// <summary> /// 应用筛选 /// </summary> /// <param name="operation"></param> /// <param name="context"></param> public void Apply(OpenApiOperation operation, OperationFilterContext context) { //获取参数类型 var type = context.ApiDescription.ActionDescriptor.Parameters.FirstOrDefault()?.ParameterType; var proty = type?.GetProperties() ?? new PropertyInfo[] { }; foreach (var item in proty) { //查找忽略Attribute var attr = item.GetCustomAttribute<JsonIgnoreAttribute>(); if (attr == null) continue; //查找忽略参数 var param = operation.Parameters.FirstOrDefault(p => p.Name == item.Name); if (param != null) operation.Parameters.Remove(param); } } }
二、配置使用筛选器
①、使用操作筛选器
builder.Services.AddSwaggerGen(options => { options.OperationFilter<SwaggerOperationFilter>(); });
②、配置忽略(隐藏)字段

如有帮助,欢迎转载,转载请注明原文链接:https://www.cnblogs.com/study10000/p/16874192.html

浙公网安备 33010602011771号