RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
利用Swashbuckle生成WebAPIHelpPages

Swashbuckle简介

Swashbuckle有两个核心组件:

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

在middleware中添加并配置Swagger

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

 
 
 
 
  1. "Swashbuckle": "6.0.0-beta902" 

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

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

 
 
 
 
  1. 在middleware中添加并配置Swagger 
  2. 首先,要将Swashbuckle添加到项目中的project.json: 
  3.  
  4. "Swashbuckle": "6.0.0-beta902" 
  5. 然后在Configure方法中添加SwaggerGen到services集合中,接着在ConfigureServices方法中,允许中间件(middleware)为生成的JSON文档和SwaggerUI提供服务。 
  6.  
  7.  
  8.  
  9. 执行dotnet run命令,并导航到http://localhost:5000/swagger/v1/swagger.json 查看描述终结点的文档。 
  10.  
  11. { 
  12.   "swagger": "2.0", 
  13.   "info": { 
  14.     "version": "v1", 
  15.     "title": "API V1" 
  16.   }, 
  17.   "basePath": "/", 
  18.   "paths": { 
  19.     "/api/User": { 
  20.       "get": { 
  21.         "tags": [ 
  22.           "User" 
  23.         ], 
  24.         "operationId": "ApiUserGet", 
  25.         "consumes": [], 
  26.         "produces": [ 
  27.           "text/plain", 
  28.           "application/json", 
  29.           "text/json" 
  30.         ], 
  31.         "responses": { 
  32.           "200": { 
  33.             "description": "Success", 
  34.             "schema": { 
  35.               "type": "array", 
  36.               "items": { 
  37.                 "$ref": "#/definitions/UserItem" 
  38.               } 
  39.             } 
  40.           } 
  41.         }, 
  42.         "deprecated": false 
  43.       }, 
  44.       "post": { 
  45.         "tags": [ 
  46.           "User" 
  47.         ], 
  48.         "operationId": "ApiUserPost", 
  49.         "consumes": [ 
  50.           "application/json", 
  51.           "text/json", 
  52.           "application/json-patch+json" 
  53.         ], 
  54.         "produces": [], 
  55.         "parameters": [ 
  56.           { 
  57.             "name": "item", 
  58.             "in": "body", 
  59.             "required": false, 
  60.             "schema": { 
  61.               "$ref": "#/definitions/UserItem" 
  62.             } 
  63.           } 
  64.         ], 
  65.         "responses": { 
  66.           "200": { 
  67.             "description": "Success" 
  68.           } 
  69.         }, 
  70.         "deprecated": false 
  71.       } 
  72.     }, 
  73.     "/api/User/{id}": { 
  74.       "get": { 
  75.         "tags": [ 
  76.           "User" 
  77.         ], 
  78.         "operationId": "ApiUserByIdGet", 
  79.         "consumes": [], 
  80.         "produces": [], 
  81.         "parameters": [ 
  82.           { 
  83.             "name": "id", 
  84.             "in": "path", 
  85.             "required": true, 
  86.             "type": "string" 
  87.           } 
  88.         ], 
  89.         "responses": { 
  90.           "200": { 
  91.             "description": "Success" 
  92.           } 
  93.         }, 
  94.         "deprecated": false 
  95.       }, 
  96.       "put": { 
  97.         "tags": [ 
  98.           "User" 
  99.         ], 
  100.         "operationId": "ApiUserByIdPut", 
  101.         "consumes": [ 
  102.           "application/json", 
  103.           "text/json", 
  104.           "application/json-patch+json" 
  105.         ], 
  106.         "produces": [], 
  107.         "parameters": [ 
  108.           { 
  109.             "name": "id", 
  110.             "in": "path", 
  111.             "required": true, 
  112.             "type": "string" 
  113.           }, 
  114.           { 
  115.             "name": "item", 
  116.             "in": "body", 
  117.             "required": false, 
  118.             "schema": { 
  119.               "$ref": "#/definitions/UserItem" 
  120.             } 
  121.           } 
  122.         ], 
  123.         "responses": { 
  124.           "200": { 
  125.             "description": "Success" 
  126.           } 
  127.         }, 
  128.         "deprecated": false 
  129.       }, 
  130.       "delete": { 
  131.         "tags": [ 
  132.           "User" 
  133.         ], 
  134.         "operationId": "ApiUserByIdDelete", 
  135.         "consumes": [], 
  136.         "produces": [], 
  137.         "parameters": [ 
  138.           { 
  139.             "name": "id", 
  140.             "in": "path", 
  141.             "required": true, 
  142.             "type": "string" 
  143.           } 
  144.         ], 
  145.         "responses": { 
  146.           "200": { 
  147.             "description": "Success" 
  148.           } 
  149.         }, 
  150.         "deprecated": false 
  151.       }, 
  152.       "patch": { 
  153.         "tags": [ 
  154.           "User" 
  155.         ], 
  156.         "operationId": "ApiUserByIdPatch", 
  157.         "consumes": [ 
  158.           "application/json", 
  159.           "text/json", 
  160.           "application/json-patch+json" 
  161.         ], 
  162.         "produces": [], 
  163.         "parameters": [ 
  164.           { 
  165.             "name": "item", 
  166.             "in": "body", 
  167.             "required": false, 
  168.             "schema": { 
  169.               "$ref": "#/definitions/UserItem" 
  170.             } 
  171.           }, 
  172.           { 
  173.             "name": "id", 
  174.             "in": "path", 
  175.             "required": true, 
  176.             "type": "string" 
  177.           } 
  178.         ], 
  179.         "responses": { 
  180.           "200": { 
  181.             "description": "Success" 
  182.           } 
  183.         }, 
  184.         "deprecated": false 
  185.       } 
  186.     }, 
  187.     "/api/Values": { 
  188.       "get": { 
  189.         "tags": [ 
  190.           "Values" 
  191.         ], 
  192.         "operationId": "ApiValuesGet", 
  193.         "consumes": [], 
  194.         "produces": [ 
  195.           "text/plain", 
  196.           "application/json", 
  197.           "text/json" 
  198.         ], 
  199.         "responses": { 
  200.           "200": { 
  201.             "description": "Success", 
  202.             "schema": { 
  203.               "type": "array", 
  204.               "items": { 
  205.                 "type": "string" 
  206.               } 
  207.             } 
  208.           } 
  209.         }, 
  210.         "deprecated": false 
  211.       }, 
  212.       "post": { 
  213.         "tags": [ 
  214.           "Values" 
  215.         ], 
  216.         "operationId": "ApiValuesPost", 
  217.         "consumes": [ 
  218.           "application/json", 
  219.           "text/json", 
  220.           "application/json-patch+json" 
  221.         ], 
  222.         "produces": [], 
  223.         "parameters": [ 
  224.           { 
  225.             "name": "value", 
  226.             "in": "body", 
  227.             "required": false, 
  228.             "schema": { 
  229.               "type": "string" 
  230.             } 
  231.           } 
  232.         ], 
  233.         "responses": { 
  234.           "200": { 
  235.             "description": "Success" 
  236.           } 
  237.         }, 
  238.         "deprecated": false 
  239.       } 
  240.     }, 
  241.     "/api/Values/{id}": { 
  242.       "get": { 
  243.         "tags": [ 
  244.           "Values" 
  245.         ], 
  246.         "operationId": "ApiValuesByIdGet", 
  247.         "consumes": [], 
  248.         "produces": [ 
  249.           "text/plain", 
  250.           "application/json", 
  251.           "text/json" 
  252.         ], 
  253.         "parameters": [ 
  254.           { 
  255.             "name": "id", 
  256.             "in": "path", 
  257.             "required": true, 
  258.             "type": "integer", 
  259.             "format": "int32" 
  260.           } 
  261.         ], 
  262.         "responses": { 
  263.           "200": { 
  264.             "description": "Success", 
  265.             "schema": { 
  266.               "type": "string" 
  267.             } 
  268.           } 
  269.         }, 
  270.         "deprecated": false 
  271.       }, 
  272.       "put": { 
  273.         "tags": [ 
  274.           "Values" 
  275.         ], 
  276.         "operationId": "ApiValuesByIdPut", 
  277.         "consumes": [ 
  278.           "application/json", 
  279.           "text/json", 
  280.           "application/json-patch+json" 
  281.         ], 
  282.         "produces": [], 
  283.         "parameters": [ 
  284.           { 
  285.             "name": "id", 
  286.             "in": "path", 
  287.             "required": true, 
  288.             "type": "integer", 
  289.             "format": "int32" 
  290.           }, 
  291.           { 
  292.             "name": "value", 
  293.             "in": "body", 
  294.             "required": false, 
  295.             "schema": { 
  296.               "type": "string" 
  297.             } 
  298.           } 
  299.         ], 
  300.         "responses": { 
  301.           "200": { 
  302.             "description": "Success" 
  303.           } 
  304.         }, 
  305.         "deprecated": false 
  306.       }, 
  307.       "delete": { 
  308.         "tags": [ 
  309.           "Values" 
  310.         ], 
  311.         "operationId": "ApiValuesByIdDelete", 
  312.         "consumes": [], 
  313.         "produces": [], 
  314.         "parameters": [ 
  315.           { 
  316.             "name": "id", 
  317.             "in": "path", 
  318.             "required": true, 
  319.             "type": "integer", 
  320.             "format": "int32" 
  321.           } 
  322.         ], 
  323.         "responses": { 
  324.           "200": { 
  325.             "description": "Success" 
  326.           } 
  327.         }, 
  328.         "deprecated": false 
  329.       } 
  330.     } 
  331.   }, 
  332.   "definitions": { 
  333.     "UserItem": { 
  334.       "type": "object", 
  335.       "properties": { 
  336.         "key": { 
  337.           "type": "string" 
  338.         }, 
  339.         "name": { 
  340.           "type": "string" 
  341.         }, 
  342.         "age": { 
  343.           "format": "int32", 
  344.           "type": "integer" 
  345.         } 
  346.       } 
  347.     } 
  348.   }, 
  349.   "securityDefinitions": {} 
  350. } 

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

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

定制&扩展

API描述信息

 
 
 
 
  1. services.ConfigureSwaggerGen(options => 
  2. { 
  3.     options.SingleApiVersion(new Info 
  4.     { 
  5.         Version = "v1", 
  6.         Title = "User Web API", 
  7.         Description = "ASP.NET Core Web API", 
  8.         TermsOfService = "None", 
  9.         Contact = new Contact { Name = "Charlie Chu", Email = "charlie.thinker@aliyun.com", Url = "http://zhuchenglin.me/" }, 
  10.         License = new License { Name = "The MIT License", Url = "http://zhuchenglin.me/" } 
  11.     }); 
  12. }); 

XML注释

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

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

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

【本文为专栏作者“朱成林”的原创稿件,转载请联系原作者】


本文名称:利用Swashbuckle生成WebAPIHelpPages
文章分享:http://aqpgk.com/article/dpjhjod.html