一、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. dw2018修改为中文

    dw2018 英文版修改为中文, 把zh_CN文件夹内的内容复制到en_US文件夹内并替换, 或者重命名zh_CN文件夹为en_US

  2. 06 Django组件-cookie与session

    一.会话跟踪技术 1.什么是会话跟踪技术 我们需要先了解一下什么是会话!可以把会话理解为客户端与服务器之间的一次会晤,在一次会晤中可能会包含多次请求和响应.例如你给10086打个电话,你就是客户端,而 ...

  3. 可横向滑动的vue tab组件

    示例 前端使用技术:框架->vue 组件>ly-tab一个用于移动端的可触摸滑动具有回弹效果的可复用Vue组件 ly-tab 介绍地址 ly-tab npm地址 使用步骤 1,引入包,定义 ...

  4. Centos7下 yum安装php扩展

    linux下, 使用thinkphp的模板标签,如 eq, gt, volist defined, present , empty等 标签时, 报错: used undefined function ...

  5. 破解sublim_Text3

    1.更改hosts文件 windows系统的hosts文件在C:\Windows\System32\drivers\etc 路径下,其他系统请自行百度 在hosts文件中加入下面两行: 127.0.0 ...

  6. Scalable Web Architecture and Distributed Systems

    转自:http://aosabook.org/en/distsys.html Scalable Web Architecture and Distributed Systems Kate Matsud ...

  7. 修改默认input(file)的样式

    以上是默认的 <input type="file" > 但是丑爆了啊同志们~~长久以来都是调用大神的代码,今天我也小试牛刀,做出了如下效果: 这样还是能接受的样子啦~ ...

  8. P1421 小玉买文具

    ... 题目描述 班主任给小玉一个任务,到文具店里买尽量多的签字笔.已知一只签字笔的价格是1元9角,而班主任给小玉的钱是a元b角,小玉想知道,她最多能买多少只签字笔呢. 输入输出格式 输入格式: 输入 ...

  9. nyoj303-序号交换

    序号互换 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 Dr.Kong设计了一个聪明的机器人卡多,卡多会对电子表格中的单元格坐标快速计算出来.单元格的行坐标是由数字编号 ...

  10. centos安装wget 及配置

    yum -y install wget #yum -y install setup 本文 #yum install perl Searching for GCC... The path "& ...