dotnet core 2.2开发项目中,常会使用Swagger UI来生成在线Api文档。

某些接口不想放到Swagger中可以这样写Filter:

    /// <summary>
/// 隐藏swagger接口特性标识
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Class)]
public partial class HiddenApiAttribute : System.Attribute { } /// <summary>
/// 隐藏接口,不生成到swagger文档展示
/// </summary>
public class HiddenApiFilter : IDocumentFilter
{
/// <summary>
/// 过滤器
/// </summary>
/// <param name="swaggerDoc"></param>
/// <param name="context"></param> public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
foreach (ApiDescription apiDescription in context.ApiDescriptions)
{
if (apiDescription.TryGetMethodInfo(out MethodInfo method))
{
if (method.ReflectedType.CustomAttributes.Any(t => t.AttributeType == typeof(HiddenApiAttribute))
|| method.CustomAttributes.Any(t => t.AttributeType == typeof(HiddenApiAttribute)))
{
string key = "/" + apiDescription.RelativePath;
if (key.Contains("?"))
{
int idx = key.IndexOf("?", System.StringComparison.Ordinal);
key = key.Substring(0, idx);
}
swaggerDoc.Paths.Remove(key);
}
}
}
}
}

  注意:他不能隐藏一个Controller中的某个Method,比如仅隐藏Post或Get方法,因为它是对路由Paths.Remove

Starts.cs中加入代码:

c.DocumentFilter<HiddenApiFilter>();

  Controller类或某个方法加入代码:

 [HiddenApi]
public class ValuesController : ControllerBase
{
...
}

  显示枚举描述

    /// <summary>
/// Add enum value descriptions to Swagger
/// </summary>
public class EnumDocumentFilter : IDocumentFilter
{
/// <inheritdoc />
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
// add enum descriptions to result models
foreach (var schemaDictionaryItem in swaggerDoc.Definitions)
{
var schema = schemaDictionaryItem.Value;
foreach (var propertyDictionaryItem in schema.Properties)
{
var property = propertyDictionaryItem.Value;
var propertyEnums = property.Enum;
if (propertyEnums != null && propertyEnums.Count > 0)
{
property.Description += DescribeEnum(propertyEnums);
}
}
}
} private static string DescribeEnum(IEnumerable<object> enums)
{
var enumDescriptions = new List<string>();
Type type = null;
foreach (var enumOption in enums)
{
if (type == null) type = enumOption.GetType();
enumDescriptions.Add($"{Convert.ChangeType(enumOption, type.GetEnumUnderlyingType())} = {Enum.GetName(type, enumOption)},{GetDescription(type, enumOption)}");
} return $"{Environment.NewLine}{string.Join(Environment.NewLine, enumDescriptions)}";
}
public static string GetDescription(Type t, object value)
{
foreach (MemberInfo mInfo in t.GetMembers())
{
if (mInfo.Name == t.GetEnumName(value))
{
foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
{
if (attr.GetType() == typeof(DescriptionAttribute))
{
return ((DescriptionAttribute)attr).Description;
}
}
}
}
return string.Empty;
}
}

  Starts.cs中加入代码:

  c.DocumentFilter<EnumDocumentFilter>();

  以上。

dotnet core swagger filter 隐藏接口和显示枚举描述的更多相关文章

  1. .Net core 下Swagger如何隐藏接口的显示

    Swagger是这个非常强大的api文档工具,通常可以用来测试接口,和查看接口,就像这样: 非常的好用和快捷,这是一个小小的demo,我们在完成系统时,发布后,外部依旧可以用/swagger访问到这个 ...

  2. dotnet core 3.0 swagger 显示枚举描述

    上一篇net core 2.2 swagger的枚举描述,core 3.0 需要升级swagger到5.0rc版,配置需要做些修改,swaager启用了OpenApi标准,之前的枚举描述方法也失效了. ...

  3. dotnet Core 调用HTTP接口,系统大量CLOSE_WAIT连接问题的分析,尚未解决。

    环境: dotnet core 1.0.1 CentOS 7.2 今天在服务器巡检的时候,发现一个服务大量抛出异常 异常信息为: LockStatusPushError&&Messag ...

  4. dotnet core 通过修改文件头的方式隐藏控制台窗口

    原文:dotnet core 通过修改文件头的方式隐藏控制台窗口 在带界面的 dotnet core 程序运行的时候就会出现一个控制台窗口,本文告诉大家使用最简单方法去隐藏控制台窗口. 最近在使用 A ...

  5. .Net WebApi接口之Swagger UI 隐藏指定接口类或方法

    swagger的一个最大的优点是能实时同步api与文档,但有些时候我们不想全部公开接口,而要隐藏或屏蔽一些接口类或方法,swagger也是支持的,只需要设置一下DocumentFilter方法. 第一 ...

  6. dotnet core 隐藏控制台

    如果写一个控制台程序,需要隐藏这个控制台程序,可以使用本文的方法 如果是在 Windows 下运行, 可以使用一些系统提供的方法隐藏控制台.如果是 Linux 下,都是控制台,就不用隐藏了 复制下面的 ...

  7. 将app接口服务器改为dotnet core承载

    昨天我的一个 app 的接口服务器挂掉了,国外的小鸡意外的翻车,连同程序和数据一起,猝不及防.我的服务端程序是 asp.net mvc ,小鸡是 256 M 的内存跑不了 windows 系统,装的 ...

  8. 【Core Swagger】.NET Core中使用swagger

    一.入门 https://www.nuget.org/packages/Swashbuckle.AspNetCore.SwaggerGen/ 1.添加核心NUGET包 Swashbuckle.AspN ...

  9. swagger webapi控制器注释不显示

    swagger是webapi文档描述及调试工具,要在asp.net mvc中使用swagger,需要安装Swashbuckle.Core这个包,安装好后会在app_start中生成SwaggerCon ...

随机推荐

  1. 理解迭代器,生成器,yield,可迭代对象

    原文:https://foofish.net/iterators-vs-generators.html 本文源自RQ作者的一篇博文,原文是Iterables vs. Iterators vs. Gen ...

  2. RabbitMQ 的 docker 镜像使用

    RabbitMQ 的 docker 镜像使用 1.下载镜像(management版本的才带有web管理界面) docker pull rabbitmq:3.7.18-management 2.创建容器 ...

  3. ssm上传文件

    ssm上传文件(上传到本地磁盘) (1)先要导入所需要的jar包或者pom文件中添加依赖 <!-- 上传 --> <dependency> <groupId>com ...

  4. 前段性能----repaint和reflow

    在前面小节,我们对网页渲染过程做了介绍,其中最后两步就是layout与paint,当渲染对象被创建并添加到树中,它们并没有位置和大小,计算这些值的过程称为layout或reflow.绘制阶段,遍历渲染 ...

  5. linux学习16 Linux用户和组管理命令演练和实战应用

    一.上集回顾 1.bash globing,IO重定向及管道 glob:*,?,[],[^] IO重定向: >,>>, 2>,2>> &>,& ...

  6. ABP 01 项目的基本运行

    原文:https://www.cnblogs.com/ldybyz/p/8441084.html 照着这篇文章弄 一般是没有什么问题的 记录一下我出现的问题,大多是没有仔细看文章. 1.无法迁移数据库 ...

  7. 【LG2839】[国家集训队]middle

    [LG2839][国家集训队]middle 题面 洛谷 题解 按照求中位数的套路,我们二分答案\(mid\),将大于等于\(mid\)的数设为\(1\),否则为\(-1\). 若一个区间和大于等于\( ...

  8. UE4的联网系统研究

    1. 物体复制 具体细节可参考官网内容:http://api.unrealengine.com/CHN/Gameplay/Networking/index.html 这里只挑部分点来展开. 首先,分为 ...

  9. Python研究

    听说Python是种高级语言,故打算研究一下,看看高级在哪 学习资源:https://www.liaoxuefeng.com/wiki/1016959663602400 由于上面所示的网站对Pytho ...

  10. 【POJ3126】Prime Path

    本题传送门 本题知识点:宽度优先搜索 题意很简单.要找一个质数变到另一个质数的最少步数,两个质数都是4位数,变的时候只能变其中一位,变了的数也仍是质数. 思路也很简单,对每一位数进行修改,如果修改后的 ...