很好用的API管理--Swagger
1.打开NuGet程序包

2.安装下面两个程序包


3.安装完后会出现SwaggerConfig.cs类,并修改里面的内容

代码:
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] namespace WebApplication1
{
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly; GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "WebApp");
c.IncludeXmlComments(GetXmlCommentsPath());
})
}
private static string GetXmlCommentsPath()
{
return $@"{System.AppDomain.CurrentDomain.BaseDirectory}\bin\WebApi.XML";
}
}
4.添加WebApi.cs类

代码:
namespace WebApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services // Web API routes
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
name: "DefaultApi1",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
); config.Routes.MapHttpRoute(
name: "DefaultApi2",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action="get",id = RouteParameter.Optional }
);
}
}
}
5.修改Global.asax类

代码:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register); //这是要加的
}
}
6.设置需要调试的类

7.设置生成的XML文档

8.运行项目,打开浏览器输入http://localhost:23092/swagger/
结果展示:

注意:
在使用Swagger WebApi文档框架时,一个Controll中写多个Post接口时,在MVC4中会提示无法识别的多个post请求,原因是WebApiConfig中默认路由配置中是没有配置{action}引起的。
【在MVC5中没有此问题,原因是MVC5有Route[" "]特性,MVC4中不支持】
解决方案:
方法1:修改WebApiConfig文件
//默认配置
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//修改后配置
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
方法2:MVC5中可以不修改默认配置文件,而使用Route特性来指定
/// <summary>
/// POST请求1
///
///api/Member
/// </summary>
/// <param name="value"></param>
public void Post([FromBody]string value)
{ } /// <summary>
/// POST请求2
/// api/Member/Login
/// </summary>
/// <param name="loginId"></param>
/// <param name="password"></param>
/// <returns></returns>
[HttpPost, Route("api/Member/Login")]
public string Login([FromBody]string loginId, [FromBody]string password)
{
return "";
}
附带:隐藏接口
在使用Swagger需要隐藏不开放的API
可以在SwaggerConfig.cs中加入
/// <summary>
/// 隐藏接口,不生成到swagger文档展示
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Class)] public partial class HiddenApiAttribute : System.Attribute { }
public class HiddenApiFilter : IDocumentFilter
{
/// <summary>
/// 重写Apply方法,移除隐藏接口的生成
/// </summary>
/// <param name="swaggerDoc">swagger文档文件</param>
/// <param name="schemaRegistry"></param>
/// <param name="apiExplorer">api接口集合</param>
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(, idx);
}
swaggerDoc.paths.Remove(key);
}
}
}
}
并且在SwaggerConfig.cs 中加入
c.DocumentFilter<HiddenApiFilter>();
搞定
很好用的API管理--Swagger的更多相关文章
- Api管理工具(spring-rest-docs)
对于app开发来说,必须需要有相应的api文档,一般最基础的就是用markdown工具来撰写api文档.当对于开发人员来说,是总会想着寻找更方便撰写,测试,对接前端开发的文档生成的工具. 其实这方面的 ...
- API 管理在云原生场景下的机遇与挑战
作者 | 张添翼 来源 | 尔达Erda公众号 云原生下的机遇和挑战 标准和生态的意义 自从 Kubernetes v1.0 于 2015 年 7 月 21 日发布,CNCF 组织随后建立以来,其 ...
- API管理之利剑 -- Eolink
随着信息化飞速增长的还有各信息系统中的应用接口( API ),API 作为信息系统内部及不同信息系统之间进行数据传输的渠道,其数量随着软件系统的不断庞大而呈指数型增长,如何管理这些 API 已经在业界 ...
- 论元数据和API管理工具
公司里面的很多部门都在广泛的采用元数据管理,也采用了公司内部开发的元数据管理工具,有些部门的实施效果一直非常好,而有些部门的效果则差强人意.这个问题,其实和软件系统开发完成进入维护阶段后成本居高不下的 ...
- API管理平台XXL-API
<API管理平台XXL-API> 一.简介 1.1 概述 XXL-API是一个简洁易用API管理平台,提供API的"管理"."文档"."M ...
- 高效、易用、功能强大的 api 管理平台
前言导读 实际环境的需求可以说是:只有你没想到,没有实现不了的,征对于目前实际开发.测试.生产等环境中,需要用到各类的接口可达几十.甚至上百个,因此,必须需要一个统一管理的工具平台来统一管理这类接口, ...
- 你不得不知的几款常用的在线API管理工具
在项目开发过程中,总会涉及到接口文档的设计编写,之前使用的都是ms office工具,不够漂亮也不直观,变更频繁的话维护成本也更高,及时性也是大问题.基于这个背景,下面介绍几个常用的API管理工具,方 ...
- 基于SpringCloud的Microservices架构实战案例-在线API管理
simplemall项目前几篇回顾: 1基于SpringCloud的Microservices架构实战案例-序篇 2基于SpringCloud的Microservices架构实战案例-架构拆解 3基于 ...
- 几款常用的在线API管理工具(是时候抛弃office编写接口文档了)
在项目开发过程中,总会涉及到接口文档的设计编写,之前使用的都是ms office工具,不够漂亮也不直观,变更频繁的话维护成本也更高,及时性也是大问题.基于这个背景,下面介绍几个常用的API管理工具,方 ...
随机推荐
- win10 +Kinect V1 1414环境配置
win10 +Kinect V1 1414环境配置 想起老Lab的机器人头顶的Kinect 安装准备 demo展示 人脸识别 照片不能够检测到人脸 可以去除背景 检测骨架 想起老Lab的机器人头顶的K ...
- Error: Cannot find module '@babel/core'
报错如下 产生原因 babel-loader和babel-core版本不对应所产生的, babel-loader 8.x对应babel-core 7.x babel-loader 7.x对应babel ...
- day 60 Django基础七之Ajax
Django基础七之Ajax 本节目录 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解) 六 同源策 ...
- PAT甲级——A1132 Cut Integer
Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long int ...
- PAT甲级——A1129 Recommendation System【25】
Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...
- PAT甲级——A1117 Eddington Number【25】
British astronomer Eddington liked to ride a bike. It is said that in order to show off his skill, h ...
- 3列滚动抽奖 jquery.slotmachine
效果图: 需引入js文件: <script src="js/jquery-3.2.0.js"></script> <script src=" ...
- PagedListCore的使用
关于在Core2.0中PagedListCore实现分页 一.引言 开发中在对大量数据展示的时候,出于对性能的考虑,我们往往是使用分页功能(用户需要那一页我们就返回给他那一页,而不是一次性加载所有的数 ...
- Go前言
Go语言为并发而生 硬件制造商正在为处理器添加越来越多的内核以来提高性能.所有数据中心都在这些处理器上运行,今天的应用程序使用多个微服务来维护数据库连接,消息队列和维护缓存.所以,开发的软件和编程语言 ...
- Django static静态配置文件
对于Django来说静态文件一般交由Web服务器处理,Django本身不处理静态文件.为了使Django开发环境能够处理静态文件,Django有和生产环境不同的静态文件配置方式. Django 版本: ...