利用Swashbuckle生成Web API Help Pages

本文将通过Swagger的.NET Core的实现封装工具Swashbuckle来生成上一篇-《创建ASP.NET Core Web API》的帮助文档。

Swashbuckle简介

Swashbuckle有两个核心组件:

  • Swashbuckle.SwaggerGen: 提供生成描述对象,方法,返回类型等JSON Swagger文档的功能。
  • Swashbuckle.SwaggerUI: 一个Swagger UI工具的嵌入式版本,可以使用上面的文档来创建可定制化的Web API的功能描述,包含内置的公共方法的测试工具。

在middleware中添加并配置Swagger

首先,要将Swashbuckle添加到项目中的project.json

"Swashbuckle": "6.0.0-beta902"

然后在Configure方法中添加SwaggerGen到services集合中,接着在ConfigureServices方法中,允许中间件(middleware)为生成的JSON文档和SwaggerUI提供服务。

执行dotnet run命令,并导航到http://localhost:5000/swagger/v1/swagger.json 查看描述终结点的文档。

{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "API V1"
},
"basePath": "/",
"paths": {
"/api/User": {
"get": {
"tags": [
"User"
],
"operationId": "ApiUserGet",
"consumes": [],
"produces": [
"text/plain",
"application/json",
"text/json"
],
"responses": {
"200": {
"description": "Success",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/UserItem"
}
}
}
},
"deprecated": false
},
"post": {
"tags": [
"User"
],
"operationId": "ApiUserPost",
"consumes": [
"application/json",
"text/json",
"application/json-patch+json"
],
"produces": [],
"parameters": [
{
"name": "item",
"in": "body",
"required": false,
"schema": {
"$ref": "#/definitions/UserItem"
}
}
],
"responses": {
"200": {
"description": "Success"
}
},
"deprecated": false
}
},
"/api/User/{id}": {
"get": {
"tags": [
"User"
],
"operationId": "ApiUserByIdGet",
"consumes": [],
"produces": [],
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "Success"
}
},
"deprecated": false
},
"put": {
"tags": [
"User"
],
"operationId": "ApiUserByIdPut",
"consumes": [
"application/json",
"text/json",
"application/json-patch+json"
],
"produces": [],
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "item",
"in": "body",
"required": false,
"schema": {
"$ref": "#/definitions/UserItem"
}
}
],
"responses": {
"200": {
"description": "Success"
}
},
"deprecated": false
},
"delete": {
"tags": [
"User"
],
"operationId": "ApiUserByIdDelete",
"consumes": [],
"produces": [],
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "Success"
}
},
"deprecated": false
},
"patch": {
"tags": [
"User"
],
"operationId": "ApiUserByIdPatch",
"consumes": [
"application/json",
"text/json",
"application/json-patch+json"
],
"produces": [],
"parameters": [
{
"name": "item",
"in": "body",
"required": false,
"schema": {
"$ref": "#/definitions/UserItem"
}
},
{
"name": "id",
"in": "path",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "Success"
}
},
"deprecated": false
}
},
"/api/Values": {
"get": {
"tags": [
"Values"
],
"operationId": "ApiValuesGet",
"consumes": [],
"produces": [
"text/plain",
"application/json",
"text/json"
],
"responses": {
"200": {
"description": "Success",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"deprecated": false
},
"post": {
"tags": [
"Values"
],
"operationId": "ApiValuesPost",
"consumes": [
"application/json",
"text/json",
"application/json-patch+json"
],
"produces": [],
"parameters": [
{
"name": "value",
"in": "body",
"required": false,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Success"
}
},
"deprecated": false
}
},
"/api/Values/{id}": {
"get": {
"tags": [
"Values"
],
"operationId": "ApiValuesByIdGet",
"consumes": [],
"produces": [
"text/plain",
"application/json",
"text/json"
],
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "integer",
"format": "int32"
}
],
"responses": {
"200": {
"description": "Success",
"schema": {
"type": "string"
}
}
},
"deprecated": false
},
"put": {
"tags": [
"Values"
],
"operationId": "ApiValuesByIdPut",
"consumes": [
"application/json",
"text/json",
"application/json-patch+json"
],
"produces": [],
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "integer",
"format": "int32"
},
{
"name": "value",
"in": "body",
"required": false,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Success"
}
},
"deprecated": false
},
"delete": {
"tags": [
"Values"
],
"operationId": "ApiValuesByIdDelete",
"consumes": [],
"produces": [],
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "integer",
"format": "int32"
}
],
"responses": {
"200": {
"description": "Success"
}
},
"deprecated": false
}
}
},
"definitions": {
"UserItem": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"name": {
"type": "string"
},
"age": {
"format": "int32",
"type": "integer"
}
}
}
},
"securityDefinitions": {}
}

该文档用来驱动Swagger UI,可以导航http://localhost:5000/swagger/ui来查看Swagger UI。

UserController里面的每个方法都可以在该页面上通过点击"Try it out!"进行测试。

定制&扩展

API描述信息

ConfigureSwaggerGen方法可以用来添加作者、版权、描述等信息。

services.ConfigureSwaggerGen(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "User Web API",
Description = "ASP.NET Core Web API",
TermsOfService = "None",
Contact = new Contact { Name = "Charlie Chu", Email = "charlie.thinker@aliyun.com", Url = "http://zhuchenglin.me/" },
License = new License { Name = "The MIT License", Url = "http://zhuchenglin.me/" }
});
});

XML注释

通过在project.json添加“xmlDoc”: true来启用XML注释。

ApplicationBasePath获取该应用的根路径,它必须为XML注释设置一个完整的路径,生成的XML注释名称基于你的应用程序的名称。

注意这个界面是通过之前生成的JSON文件来驱动的,所有的这些API描述信息和XML注释都会写入到这个文件中。

个人博客

我的个人博客

利用Swashbuckle生成Web API Help Pages的更多相关文章

  1. 利用Swashbuckle生成Web API Help Pages

    利用Swashbuckle生成Web API Help Pages 这系列文章是参考了.NET Core文档和源码,可能有人要问,直接看官方的英文文档不就可以了吗,为什么还要写这些文章呢? 原因如下: ...

  2. Loadrunner 脚本开发-利用Loadrunner生成Web service测试脚本

    脚本开发-利用Loadrunner生成Web service测试脚本 1.选择协议--Web Service,如下图 2.导入服务 入口1:点击Manage Services ->弹出窗中选择“ ...

  3. ASP.NET Web API Help Pages using Swagger

    Understanding the various methods of an API can be a challenge for a developer when building a consu ...

  4. web API help pages with Swagger / OpenAPI

    https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetc ...

  5. 使用ASP.NET Web API Help Pages 创建在线接口文档

    操作步骤 1.新建Web API项目 2.在项目Areas文件夹下找到以下文件,取消注释图中代码. 3.右键解决方案,属性,如图设置. 4.运行程序,点击右上角API 接口列表: 详情-无参数: 详情 ...

  6. Swagger+AutoRest 生成web api客户端(.Net)

    简介 对于.net来说,用web api来构建服务是一个不错的选择,都是http请求,调用简单,但是如果真的要在程序中调用,则还有些工作要做,比如我们需要手写httpClient调用,并映射Model ...

  7. 【ASP.NET Web API2】利用HttpClient调用Web API(TODO)

    参照: 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用 纯属记录一下遇到的问题: 我们利用HttpClient来调用自宿主方式寄宿的Web API.HttpCl ...

  8. 利用DelegatingHandler实现Web Api 的Api key校验

    客户端在请求Web Api时可以有以下两种方式提供API key 基于Querystring提供Api key http://localhost:57967/Api/Values?key=12345 ...

  9. 自动生成web api接口文档

    然后打开web程序,访问ip:port/Help. 为什么可以直接输入Help就能访问呢,因为这个插件本身已经配置了路径,如下. public class HelpPageAreaRegistrati ...

随机推荐

  1. 关于Mybatis浅谈

    1. 历史 MyBatis本是apache的一个开源项目iBatis. 2010年这个项目由apache software foundation 迁移到了google code,并且取名为MyBati ...

  2. 避免切换横竖屏Fragment的重复加载导致UI混乱

    当我们切换横竖屏时 Activity的生命周期就会重走一遍,自然 其中的Fragment的生命周期也就重新走了一遍,实践证明 当熄屏 再开屏时 Fragment的生命周期也会重走一遍 解决方案: an ...

  3. Spring bean的生命流程

    Spring 是一个轻量级的 J2EE 开源框架,其目标是降低企业级应用开发难度,提高企业级应用开发效率.在日程开发中,我们会经常使用 Spring 框架去构建应用.所以作为一个经常使用的框架,了解其 ...

  4. 5N - 考试排名

    C++编程考试使用的实时提交系统,具有即时获得成绩排名的特点.它的功能是怎么实现的呢? 我们做好了题目的解答,提交之后,要么“AC”,要么错误,不管怎样错法,总是给你记上一笔,表明你曾经有过一次错误提 ...

  5. SSH 免密码登陆到多台机器

    场景: 需要从主机1.100免密码多了到1.115及1.116 实现: 登陆1.100 $ cd ~/.ssh/ $ ssh-keygen -t rsa 然后三个回车 会有两个文件产生,id_rsa ...

  6. java常用集合浅层解析-面试必备

    ArrayList 1.动态数组 2.线程不安全 3.存储空间连续 4.查询快,添加删除慢 构造方法 /** + Shared empty array instance used for defaul ...

  7. Which Queue Pair type to use?

    Which Queue Pair type to use? Contents [hide] 1 Reliable Connected (RC) QP 2 Unreliable Connected (U ...

  8. CentOS 7 minimal网络配置

    centos最小化安装没有ifconfig命令,可以使用  ip addr  查看网络信息,习惯ifconfig的用户,则使用 yum -y install net-tools 即可 ; 如果yum不 ...

  9. RNA-seq中的基因表达量计算和表达差异分析

    RNA-seq中的基因表达量计算和表达差异分析 差异分析的步骤:1)比对:2) read count计算:3) read count的归一化:4)差异表达分析: 背景知识:1)比对:普通比对: BWA ...

  10. ListView点击事件失效(item里面有button按钮控件)解决方法

    ListView点击事件失效解决方法: 一般出现这个情况,就是你的item里面有按钮的点击事件,你的item里面有button控件,button控件是抢占焦点的,只要在你的item布局里面这样子写就可 ...