一、why

  在swagger ui界面中有时候不想显示某些api,通过下面的方式可以实现。

  1.1、新建一个类实现IDocumentFilter接口
using Swashbuckle.Swagger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Description; namespace InvoiceApi.InvoiceHelper
{
/// <summary>
/// 隐藏接口,不生成到swagger文档展示
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Class)]
public partial class HiddenApiAttribute : System.Attribute { }
public class HiddenApiFilter : IDocumentFilter
{ public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
foreach (ApiDescription apiDescription in apiExplorer.ApiDescriptions)
{
if (Enumerable.OfType<HiddenApiAttribute>(apiDescription.GetControllerAndActionAttributes<HiddenApiAttribute>()).Any())
{
string key = "/" + apiDescription.RelativePath;
if (key.Contains("?"))
{
int idx = key.IndexOf("?", System.StringComparison.Ordinal);
key = key.Substring(0, idx);
}
swaggerDoc.paths.Remove(key);
}
}
}
}
}
  1.2、在SwaggerConfig修改配置

  1.3、在想隐藏的api上面添加特性 [HiddenApi]
using InvoiceApi.InvoiceHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http; namespace InvoiceApi.Controllers
{ public class DownController : ApiController
{
/// <summary>
/// 下载api参数文档
/// </summary>
/// <returns></returns>
[HttpGet]
[HiddenApi] public HttpResponseMessage DownloadDoc()
{
try
{
var FilePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/Doc/api参数说明.docx");
var stream = new FileStream(FilePath, FileMode.Open);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "纸质发票参数API文档.doc", };
return response;
}
catch
{
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
} }
}

  

Swagger 隐藏具体API的更多相关文章

  1. Adding Swagger to Web API project

    Adding Swagger to Web API project. All source code for this series can be found here. When you creat ...

  2. 关于android源码中的APP编译时引用隐藏的API出现的问题

    今天在编译android源码中的计算器APP时发现,竟然无法使用系统隐藏的API,比如android.os.ServiceManager中的API,引用这个类时提示错误,记忆中在android源码中的 ...

  3. Swagger UI及 Swagger editor教程 API文档搭配 Node使用

    swagger ui 是一个在线文档生成和测试的利器,目前发现最好用的.为啥好用呢?打开 demo,支持API自动生成同步的在线文档些文档可用于项目内部API审核方便测试人员了解 API这些文档可作为 ...

  4. Katalon Studio之swagger中的API导入

    约束条件: swagger中一定要在注解@ApiOperation中设置nickname的唯一值,例如: @ApiOperation(value="新增用户",notes=&quo ...

  5. android 如何调用 隐藏的 API 接口

    怎样查看并且使用 Android 隐藏 API 和内部 APIhttps://www.jianshu.com/p/fbf45770ecc8 android 隐藏API显式调用以及内部资源使用方法htt ...

  6. Swagger UI教程 API 文档神器 搭配Node使用

    ASP.NET Web API 使用Swagger生成在线帮助测试文档 Swagger 生成 ASP.NET Web API 前言 swagger ui是一个API在线文档生成和测试的利器,目前发现最 ...

  7. Swagger 生成 PHP API 接口文档

    Swagger 生成 PHP API 接口文档 Lumen微服务生成Swagger文档 1.概况 有同学反馈写几十个接口文档需要两天的工作量, 随着多部门之间的协作越来越频繁, 维护成本越来越高, 文 ...

  8. go实践之swagger自动生成api文档

    文章目录 go实践之swagger自动生成api文档 1.安装需要用到的包 2.接口代码支持swagger 3. 生成swagger接口 go实践之swagger自动生成api文档 作为一个后端开发, ...

  9. Spring Boot 集成 Swagger 生成 RESTful API 文档

    原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...

随机推荐

  1. 获取listview的高度代码

    public int getTotalHeightofListView(ListView listView) { ListAdapter mAdapter = (ListAdapter) listVi ...

  2. jquey中的事件绑定

    三种方法: $(selector).live(events, data, handler);                // jQuery 1.3+ $(document).delegate(se ...

  3. Thread pool引起的程序连接数据库响应慢

    数据库版本:percona-mysql 5.6.16 ​在很长一段时间,都会出现程序连接数据库,出现响应慢的情况,正常在几到几十毫秒之间,但是偶尔会出现上百毫秒的情况: 开始由于开发重新设置并调整过程 ...

  4. 【图文】Excel中vlookup函数的使用方法

    今天统计数据,用到了Excel中vlookup函数,第一次使用当然少不了百度,经过反复研究后,算是解决了问题,现整理成文档. 一.实现效果 Sheet1 Sheet2   注:上图中sheet1商品条 ...

  5. ubuntu 搭建简易的https网站

    ubuntu 搭建简易的https网站 环境:ubuntu 12.04.5 openssl (1)创建一个ssl的保存路径 sudo mkdir /opt/nginx/ssl (2)生存密钥sudo ...

  6. (转)RabbitMQ学习之Headers交换类型(java)

    http://blog.csdn.net/zhu_tianwei/article/details/40923131 Headers类型的exchange使用的比较少,它也是忽略routingKey的一 ...

  7. ZBrush的双十一来了,然鹅...

    不管是“光棍节”还是“剁手节” 似乎和我都没有什么关系 事实证明,我错了 今早竟然有不识趣的人发红包祝我单身快乐 纳尼,这是唱的哪一出? 我能直接怼回去,说不领么? 但好像又不是我的风格 哎,一个红包 ...

  8. VMware 虚拟化技术 创建虚拟机

    原文地址:https://www.linuxidc.com/Linux/2017-03/141972.htm 云最成熟的架构是IaaS(Infrastructure as a Service),其中用 ...

  9. cent os 安装mariaDB / mySQL 之后初始化的命令

      #安装mysql mysql-server,默认安装的是开源的mariaDB和它的server,mariadb-server,安装源中可能有找不到的,就换个名字再找找 yum install -y ...

  10. centos7编译安装mysql5.6

    先安装如下依赖包: $ yum -y install make gcc-c++ cmake bison-devel  ncurses-devel 下载MySQL5.6.14安装包,https://pa ...