Swagger 隐藏具体API
一、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的更多相关文章
- 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 ...
- 关于android源码中的APP编译时引用隐藏的API出现的问题
今天在编译android源码中的计算器APP时发现,竟然无法使用系统隐藏的API,比如android.os.ServiceManager中的API,引用这个类时提示错误,记忆中在android源码中的 ...
- Swagger UI及 Swagger editor教程 API文档搭配 Node使用
swagger ui 是一个在线文档生成和测试的利器,目前发现最好用的.为啥好用呢?打开 demo,支持API自动生成同步的在线文档些文档可用于项目内部API审核方便测试人员了解 API这些文档可作为 ...
- Katalon Studio之swagger中的API导入
约束条件: swagger中一定要在注解@ApiOperation中设置nickname的唯一值,例如: @ApiOperation(value="新增用户",notes=&quo ...
- android 如何调用 隐藏的 API 接口
怎样查看并且使用 Android 隐藏 API 和内部 APIhttps://www.jianshu.com/p/fbf45770ecc8 android 隐藏API显式调用以及内部资源使用方法htt ...
- Swagger UI教程 API 文档神器 搭配Node使用
ASP.NET Web API 使用Swagger生成在线帮助测试文档 Swagger 生成 ASP.NET Web API 前言 swagger ui是一个API在线文档生成和测试的利器,目前发现最 ...
- Swagger 生成 PHP API 接口文档
Swagger 生成 PHP API 接口文档 Lumen微服务生成Swagger文档 1.概况 有同学反馈写几十个接口文档需要两天的工作量, 随着多部门之间的协作越来越频繁, 维护成本越来越高, 文 ...
- go实践之swagger自动生成api文档
文章目录 go实践之swagger自动生成api文档 1.安装需要用到的包 2.接口代码支持swagger 3. 生成swagger接口 go实践之swagger自动生成api文档 作为一个后端开发, ...
- Spring Boot 集成 Swagger 生成 RESTful API 文档
原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...
随机推荐
- 读《Android电视机(机顶盒)初次开发的一些经验分享》后的笔记
原文: http://blog.csdn.net/tanghongchang123/article/details/52982818 一.基本命令: 1.adb connect [ip] 2. adb ...
- 在maven项目结构下对于Resources目录下文件的存取
在maven项目中,文件结构如下: proj ---src ----main ----java ----Main.java ----resources ----userFile.properties ...
- Multitier architecture-n-tier architecture
In software engineering, multitier architecture (often referred to as n-tier architecture) or multil ...
- 应用二:Vue之ElementUI Form表单校验
(注:本文适用于有一定Vue基础或开发经验的读者,文章就知识点的讲解不一定全面,但却是开发过程中很实用的) 表单校验是前端开发过程中最常用到的功能之一,根据个人的工作经验总结在此对表单校验功能的基 ...
- jquery选择器的一些处理
本文不讨论用jquery选择器具体怎么选择页面元素,而讨论选择元素后后的一些处理 jquery的选择器选择元素的时候,即使没有选择到指定的对象,页面并不会报错,例子: <!doctype htm ...
- 洛谷P1563 玩具谜题 简单模拟
没意义,注意方向别判错. Code: #include<cstdio> #include<cstring> using namespace std; const int max ...
- 网络教程(8)IP协议
两台计算机间不是直接连接的,所以不能使用PPP协议,那么它们是怎么链接的呢? host A想把信息传给host B ethernet帧中的目标mac地址传到SFO后开始使用PPP协议 [Media ...
- BZOJ 3028 食物 (生成函数+数学题)
题面:BZOJ传送门 题目让我们求这些物品在合法范围内任意组合,一共组合出$n$个物品的方案数 考虑把每种食物都用生成函数表示出来,然后用多项式乘法把它们乘起来,第$n$项的系数就是方案数 汉堡:$1 ...
- [luogu2585 ZJOI2006] 三色二叉树 (树形dp)
传送门 Description Input 输入文件名:TRO.IN 输入文件仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件名:TRO.OUT 输出文件也只有一行,包 ...
- [HTML5] Why ARIA?
For some reason, you build a custom checkbox component, if without ARIA in mind, basiclly this site ...