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

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

  1. /// <summary>
  2. /// 隐藏swagger接口特性标识
  3. /// </summary>
  4. [System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Class)]
  5. public partial class HiddenApiAttribute : System.Attribute { }
  6.  
  7. /// <summary>
  8. /// 隐藏接口,不生成到swagger文档展示
  9. /// </summary>
  10. public class HiddenApiFilter : IDocumentFilter
  11. {
  12. /// <summary>
  13. /// 过滤器
  14. /// </summary>
  15. /// <param name="swaggerDoc"></param>
  16. /// <param name="context"></param>
  17.  
  18. public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
  19. {
  20. foreach (ApiDescription apiDescription in context.ApiDescriptions)
  21. {
  22. if (apiDescription.TryGetMethodInfo(out MethodInfo method))
  23. {
  24. if (method.ReflectedType.CustomAttributes.Any(t => t.AttributeType == typeof(HiddenApiAttribute))
  25. || method.CustomAttributes.Any(t => t.AttributeType == typeof(HiddenApiAttribute)))
  26. {
  27. string key = "/" + apiDescription.RelativePath;
  28. if (key.Contains("?"))
  29. {
  30. int idx = key.IndexOf("?", System.StringComparison.Ordinal);
  31. key = key.Substring(0, idx);
  32. }
  33. swaggerDoc.Paths.Remove(key);
  34. }
  35. }
  36. }
  37. }
  38. }

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

Starts.cs中加入代码:

  1. c.DocumentFilter<HiddenApiFilter>();

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

  1. [HiddenApi]
  2. public class ValuesController : ControllerBase
  3. {
  4. ...
  5. }

  显示枚举描述

  1. /// <summary>
  2. /// Add enum value descriptions to Swagger
  3. /// </summary>
  4. public class EnumDocumentFilter : IDocumentFilter
  5. {
  6. /// <inheritdoc />
  7. public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
  8. {
  9. // add enum descriptions to result models
  10. foreach (var schemaDictionaryItem in swaggerDoc.Definitions)
  11. {
  12. var schema = schemaDictionaryItem.Value;
  13. foreach (var propertyDictionaryItem in schema.Properties)
  14. {
  15. var property = propertyDictionaryItem.Value;
  16. var propertyEnums = property.Enum;
  17. if (propertyEnums != null && propertyEnums.Count > 0)
  18. {
  19. property.Description += DescribeEnum(propertyEnums);
  20. }
  21. }
  22. }
  23. }
  24.  
  25. private static string DescribeEnum(IEnumerable<object> enums)
  26. {
  27. var enumDescriptions = new List<string>();
  28. Type type = null;
  29. foreach (var enumOption in enums)
  30. {
  31. if (type == null) type = enumOption.GetType();
  32. enumDescriptions.Add($"{Convert.ChangeType(enumOption, type.GetEnumUnderlyingType())} = {Enum.GetName(type, enumOption)},{GetDescription(type, enumOption)}");
  33. }
  34.  
  35. return $"{Environment.NewLine}{string.Join(Environment.NewLine, enumDescriptions)}";
  36. }
  37. public static string GetDescription(Type t, object value)
  38. {
  39. foreach (MemberInfo mInfo in t.GetMembers())
  40. {
  41. if (mInfo.Name == t.GetEnumName(value))
  42. {
  43. foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
  44. {
  45. if (attr.GetType() == typeof(DescriptionAttribute))
  46. {
  47. return ((DescriptionAttribute)attr).Description;
  48. }
  49. }
  50. }
  51. }
  52. return string.Empty;
  53. }
  54. }

  Starts.cs中加入代码:

  1.   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. C#和PHP加密结果一致的DES加密解密算法。php实现和c#一致的DES加密解密

    DES加密算法 des对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码(称为对称密码),是一种对称加密 ...

  2. JAVA之Socket通讯

    Server.java: Client.java Server console:(先启动服务器,再启动客户端)  服务器读取了客户端发来的hello server: Client console:客户 ...

  3. httprunner学习11-辅助函数debugtalk.py

    前言 在httprunner里面,每个 YAML / JSON 文件的脚本都是独立运行的,有时候我们希望能跨文件使用公用的参数. 比如登录生成一个token,后面的用例都可以去引用这个token值,或 ...

  4. 斐波那契数性质 gcd(F[n],F[m])=F[gcd(n,m)]

    引理1 结论: \[F(n)=F(m)F(n-m+1)+F(m-1)F(n-m)\] 推导: \[ \begin{aligned} F(n) &= F(n-1)+F(n-2) \\ & ...

  5. Python基础知识笔记-作用域

    Python 中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的. 变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名称.Python的作用域一共有4种,分别是: ...

  6. new.target元属性 | 分别用es5、es6 判断一个函数是否使用new操作符

    函数内部有两个方法 [[call]] 和 [[construct]] (箭头函数没有这个方法),当使用new 操作符时, 函数内部调用 [[construct]], 创建一个新实例,this指向这个实 ...

  7. Gym100676 H. Capital City

    感觉题目都已经快把正解给说出来了...strongly connected的两个点的消耗为0,其实就是同一个边双连通分量里面的点消耗为0.然后缩一下点,再树形DP一下就完了.第一次写边双,但感觉挺简单 ...

  8. js字符串转换为JSON

    1. json字符串 jsStr =  “{"a":'xxx', "b":'yyy'}” JSON.parse(jsStr); //可以将json字符串转换成j ...

  9. Numpy | 01 简介

    NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库. NumPy 是一个运行速度非常快的数学库 ...

  10. GoCN每日新闻(2019-10-27)

    GoCN每日新闻(2019-10-27) 1. Golab(意大利GopherCon)2019见闻 http://fedepaol.github.io/blog/2019/10/23/golab-20 ...