DoNetCore Web Api 采用Swagger进行接口文档管理
第一步:创建API项目 步骤这里不说明
第二步:就是Nuget 包,
两种方式:1、工具-》Nuget管理-》程序包管理控制台 Install-Package Swashbuckle.AspNetCore
2、工具-》Nuget管理-》管理Nuget包... 或者右击项目。。。 输入 Swashbuckle.AspNetCore
第三步:全局配置,这里以最简单的配置为例
在Startup.cs 文件下 ConfigureService 下添加如下代码
services.AddSwaggerGen(opt => {
opt.SwaggerDoc("v1", new Info { Title = "MyFirstApi", Version = "v1" });
});
在Configure 下添加如下代码
app.UseSwagger();
app.UseSwaggerUI(opt =>
{
opt.SwaggerEndpoint("/swagger/v1/swagger.json", "MyFirstApi");
});
第四步:编写自己的接口
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; namespace DoNetCoreApiSwagger.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class userController : ControllerBase
{
private static List<User> listUser = null;
public userController()
{
listUser.AddRange(new List<User>
{
new User(){ Id=1,userName="zhansan"}
});
}
[HttpGet("getuserlist")]
public async Task<object> GetUserList(int userid)
{
return await Task.Run<object>(()=>
{
return new {code=200,data= listUser.FindAll(a=>a.Id==userid) };
});
} [HttpPost("adduser")]
public async Task<object> AddUser([FromBody]User user)
{
return await Task.Run<object>(() =>
{
listUser.Add(user);
return new { code = 200, data="添加成功" };
});
}
} public class User
{
public int Id { get; set; }
public string userName { get; set; }
}
}
第五步:配置路由,但这里不进行配置了,就以修改文件的形式进行调整
修改launchSettings.json 文件
如下:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5000",
"sslPort": 0
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"DoNetCoreApiSwagger": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5000"
}
}
}
DoNetCore Web Api 采用Swagger进行接口文档管理的更多相关文章
- api接口测试工具和接口文档管理工具
api接口测试工具和接口文档管理工具 1.postman(https://www.getpostman.com) Postman 是一个很强大的 API调试.Http请求的工具.她可是允许用户发送任何 ...
- Web Api使用Swagger提供在线文档
1.添加Swashbuckle引用 2.生成XML文件 3.添加XML解析,在接口添加注释信息 4.运行项目输入地址 http://localhost:58254/swagger
- asp.net core 使用 swagger 生成接口文档
参考地址:http://www.cnblogs.com/daxnet/p/6181366.html http://www.jianshu.com/p/fa5a9b76f3ed 微软参考文档:https ...
- 接口文档管理工具-Postman、Swagger、RAP(转载)
接口文档管理工具-Postman.Swagger.RAP 转自:http://www.51testing.com/html/10/n-3715910.html 在项目开发测试中,接口文档是贯穿始终的. ...
- Spring Boot 集成 Swagger 构建接口文档
在应用开发过程中经常需要对其他应用或者客户端提供 RESTful API 接口,尤其是在版本快速迭代的开发过程中,修改接口的同时还需要同步修改对应的接口文档,这使我们总是做着重复的工作,并且如果忘记修 ...
- .net core 使用 swagger 生成接口文档
微软参考文档:https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs= ...
- Go语言使用swagger生成接口文档
swagger介绍 Swagger本质上是一种用于描述使用JSON表示的RESTful API的接口描述语言.Swagger与一组开源软件工具一起使用,以设计.构建.记录和使用RESTful Web服 ...
- webapi 利用webapiHelp和swagger生成接口文档
webapi 利用webapiHelp和swagger生成接口文档.均依赖xml(需允许项目生成注释xml) webapiHelp:微软技术自带,仅含有模块.方法.请求-相应参数的注释. swagge ...
- Api接口文档管理工具,你知道哪些呢?
上周看到有人在我的Github开源项目中提了个issue,说是否考虑接入swagger.那今天我就用swagger与其他接口文档工具做对比,同时说说Api接口文档工具的那点事.如今,在前后端分离开发的 ...
随机推荐
- 分布式事务一2PC
分布式事务解决方案之2PC(两阶段提交) 前面已经学习了分布式事务的基础理论,以理论为基础,针对不同的分布式场景业界常见的解决方案有2PC.TCC.可靠消息最终一致性.最大努力通知这几种. 3.1.什 ...
- pipeline配置sonar和自动化
1.sonar配置webhooks, 2.url填写jenkins的地址:http://jenkinsurl/sonarqube-webhook/ 3.前提:jenkins配置好sonar的scann ...
- 在idea中打开maven项目pom.xml未识别
在idea中打开maven项目pom.xml没有识别出来,导致idea不能自动下载依赖包, 解决办法是选中pom.xml文件,右键-" add as maven project"
- Ajax跨域请求附带Cookie/Ajax跨域请求附带身份凭证
一.跨域请求中默认不带cookie等验证凭证 尤其对于post请求. 对于ajax请求,其中post,get都可以正常访问. withCredentials: false, // 允许携带cookie ...
- vue获取不到后端返回的响应头
Response.ContentType = EPPlusHelpler.ExcelContentType; Response.Headers.Add("FileName", fi ...
- Vue框架(四)——路由跳转、路由传参、cookies、axios、跨域问题、element-ui模块
路由跳转 三种方式: $router.push / $router.go / router-link to this.$router.push('/course'); this.$router.pus ...
- Linux基础(03)gdb调试
1. 安装GDB增强工具 (gef) * GDB的版本大于7.7 * wget -q -O- https://github.com/hugsy/gef/raw/master/scripts/gef.s ...
- bind2nd
bind2nd template <class Operation,class T> binder2nd <Operation> bind2nd(const Operation ...
- python递归函数和河内塔问题
关于递归函数: 函数内部调用自身的函数. 以n阶乘为例: f(n) = n ! = 1 x 2 x 3 x 4 x...x(n-1)x(n) = n x (n-1) ! def factorial(n ...
- MySQL学习一:建表
目标:创建三张表,学生表student(sid,name,gender), 课程表course(cid,name), 分数mark(mid, sid, cid, gender); 要求sid, cid ...