使用 Swagger 生成 ASP.NET Web API 在线帮助测试文档

原文:ASP.NET Web API Help Pages using Swagger
作者:Shayne Boyer
翻译:谢炀(kiler)
翻译:许登洋(Seay)

对于开发人员来说,构建一个消费应用程序时去了解各种各样的 API 是一个巨大的挑战。

在你的 Web API 项目中使用 Swagger 的 .NET Core 封装 Swashbuckle 可以帮助你创建良好的文档和帮助页面。 Swashbuckle 可以通过修改 Startup.cs 作为一组 NuGet 包方便的加入项目。

  • Swashbuckle 是一个开源项目,为使用 ASP.NET Core MVC 构建的 Web APIs 生成 Swagger 文档。
  • Swagger 是一个机器可读的 RESTful API 表现层,它可以支持交互式文档、客户端 SDK 的生成和可被发现。

本教程基于 用 ASP.NET Core MVC 和 Visual Studio 创建你的第一个 Web API 文档的例子构建。如果需要对应的代码,在这里 https://github.com/aspnet/Docs/tree/master/aspnet/tutorials/first-web-api/sample 下载示例。

 

开始入门

Swashbuckle 有两个核心的组件

  • Swashbuckle.SwaggerGen : 提供生成描述对象、方法、返回类型等的 JSON Swagger 文档的功能。
  • Swashbuckle.SwaggerUI : 是一个嵌入式版本的 Swagger UI 工具,使用 Swagger UI 强大的富文本表现形式来可定制化的来描述 Web API 的功能,并且包含内置的公共方法测试工具。
 

NuGet 包

你可以通过以下方式添加 Swashbuckle:

  • 通过 Package Manager 控制台:

    复制代码
    1. Install-Package Swashbuckle -Pre
     
  • 在 project.json 中添加 Swashbuckle:

    复制代码
    1. "Swashbuckle": "6.0.0-beta902"
     
  • 在 Visual Studio 中:
    • 右击你的项目 Solution Explorer > Manage NuGet Packages
    • 在搜索框中输入 Swashbuckle
    • 点击 “Include prerelease”
    • 设置 Package source 为 nuget.org
    • 点击 Swashbuckle 包并点击 Install
 

在中间件中添加并配置 Swagger

在 Configure 方法里面把 SwaggerGen 添加到 services 集合,并且在 ConfigureServices 方法中,允许中间件提供和服务生成 JSON 文档以及 SwaggerUI。

复制代码
  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. // Add framework services.
  4. services.AddMvc();
  5. services.AddLogging();
  6. // Add our repository type
  7. services.AddSingleton<ITodoRepository, TodoRepository>();
  8. // Inject an implementation of ISwaggerProvider with defaulted settings applied
  9. services.AddSwaggerGen();
  10. }
  11. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  12. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  13. {
  14. app.UseMvcWithDefaultRoute();
  15. // Enable middleware to serve generated Swagger as a JSON endpoint
  16. app.UseSwagger();
  17. // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
  18. app.UseSwaggerUi();
  19. }
 

在 Visual Studio 中, 点击 ^F5 启动应用程序并导航到 http://localhost:<random_port>/swagger/v1/swagger.json 页面可以看成生成的终端描述文档。

提示
Microsoft Edge,Google Chrome 以及 Firefox 原生支持显示 JSON 文档。 Chrome 的扩展会格式化文档使其更易于阅读。 下面的例子是简化版的。

复制代码
  1. {
  2. "swagger": "2.0",
  3. "info": {
  4. "version": "v1",
  5. "title": "API V1"
  6. },
  7. "basePath": "/",
  8. "paths": {
  9. "/api/Todo": {
  10. "get": {
  11. "tags": [
  12. "Todo"
  13. ],
  14. "operationId": "ApiTodoGet",
  15. "consumes": [],
  16. "produces": [
  17. "text/plain",
  18. "application/json",
  19. "text/json"
  20. ],
  21. "responses": {
  22. "200": {
  23. "description": "OK",
  24. "schema": {
  25. "type": "array",
  26. "items": {
  27. "$ref": "#/definitions/TodoItem"
  28. }
  29. }
  30. }
  31. },
  32. "deprecated": false
  33. },
  34. "post": {
  35. ...
  36. }
  37. },
  38. "/api/Todo/{id}": {
  39. "get": {
  40. ...
  41. },
  42. "put": {
  43. ...
  44. },
  45. "delete": {
  46. ...
  47. },
  48. "definitions": {
  49. "TodoItem": {
  50. "type": "object",
  51. "properties": {
  52. "key": {
  53. "type": "string"
  54. },
  55. "name": {
  56. "type": "string"
  57. },
  58. "isComplete": {
  59. "type": "boolean"
  60. }
  61. }
  62. }
  63. },
  64. "securityDefinitions": {}
  65. }
 

这个文档用来驱动 Swagger UI,可以通过访问 http://localhost:<random_port>/swagger/ui 页面来查看。

所有的 ToDo controller 的方法都是可以在 UI 上面进行测试。点击方法可以展开对应的区域,输入所需的参数并且点击 “Try it out!” 。

 

自定义与可扩展性

Swagger 不仅是显示 API 的一个简单方法,而且有可选项:文档对象模型,以及自定义交互 UI 来满足你的视觉感受或者设计语言。

 

API 信息和描述

ConfigureSwaggerGen 方法用来添加文档信息。例如:作者,版权,描述。

复制代码
  1. services.ConfigureSwaggerGen(options =>
  2. {
  3. options.SingleApiVersion(new Info
  4. {
  5. Version = "v1",
  6. Title = "ToDo API",
  7. Description = "A simple example ASP.NET Core Web API",
  8. TermsOfService = "None",
  9. Contact = new Contact { Name = "Shayne Boyer", Email = "", Url = "http://twitter.com/spboyer"},
  10. License = new License { Name = "Use under LICX", Url = "http://url.com" }
  11. });
  12. });
 

下图展示了 Swagger UI 显示添加的版本信息。

 

XML 注释

为了启用 XML 注释, 在 Visual Studio 中右击项目并且选择 Properties 在 Output Settings 区域下面点击 XML Documentation file 。

另外,你也可以通过在 project.json 中设置 “xmlDoc”: true 来启用 XML 注释。

复制代码
  1. "buildOptions": {
  2. "emitEntryPoint": true,
  3. "preserveCompilationContext": true,
  4. "xmlDoc": true
  5. },
 

配置 Swagger 使用生成的 XML 文件。

提示
对于 Linux 或者 非 Windows 操作系统,文件名和路径是大小写敏感的。 所以本例中的 ToDoApi.XML 在 Windows 上可以找到但是 CentOS 就无法找到。

复制代码
  1. // This method gets called by the runtime. Use this method to add services to the container.
  2. public void ConfigureServices(IServiceCollection services)
  3. {
  4. // Add framework services.
  5. services.AddMvc();
  6. services.AddLogging();
  7. // Add our repository type.
  8. services.AddSingleton<ITodoRepository, TodoRepository>();
  9. // Inject an implementation of ISwaggerProvider with defaulted settings applied.
  10. services.AddSwaggerGen();
  11. // Add the detail information for the API.
  12. services.ConfigureSwaggerGen(options =>
  13. {
  14. options.SingleApiVersion(new Info
  15. {
  16. Version = "v1",
  17. Title = "ToDo API",
  18. Description = "A simple example ASP.NET Core Web API",
  19. TermsOfService = "None",
  20. Contact = new Contact { Name = "Shayne Boyer", Email = "", Url = "http://twitter.com/spboyer"},
  21. License = new License { Name = "Use under LICX", Url = "http://url.com" }
  22. });
  23. //Determine base path for the application.
  24. var basePath = PlatformServices.Default.Application.ApplicationBasePath;
  25. //Set the comments path for the swagger json and ui.
  26. options.IncludeXmlComments(basePath + "\\TodoApi.xml");
  27. });
  28. }
  29. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  30. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  31. {
  32. app.UseStaticFiles();
  33. app.UseMvcWithDefaultRoute();
  34. // Enable middleware to serve generated Swagger as a JSON endpoint.
  35. app.UseSwagger();
  36. // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
  37. app.UseSwaggerUi();
  38. }
 

在上面的代码中,ApplicationBasePath 获取到应用程序的根路径,是需要设置 XML 注释文件的完整路径。 TodoApi.xml 仅适用于本例,生成的XML注释文件的名称是基于你的应用程序名称。

为方法添加的三斜线注释(C# 文档注释格式)文字会作为描述显示在 Swagger UI 对应方法的头区域。

复制代码
  1. /// <summary>
  2. /// Deletes a specific TodoItem.
  3. /// </summary>
  4. /// <param name="id"></param>
  5. [HttpDelete("{id}")]
  6. public void Delete(string id)
  7. {
  8. TodoItems.Remove(id);
  9. }
 

请注意,UI 是由生成的 JSON 文件驱动的,这些注释也会包含在文件中。

复制代码
  1. "delete": {
  2. "tags": [
  3. "Todo"
  4. ],
  5. "summary": "Deletes a specific TodoItem",
  6. "operationId": "ApiTodoByIdDelete",
  7. "consumes": [],
  8. "produces": [],
  9. "parameters": [
  10. {
  11. "name": "id",
  12. "in": "path",
  13. "description": "",
  14. "required": true,
  15. "type": "string"
  16. }
  17. ],
  18. "responses": {
  19. "204": {
  20. "description": "No Content"
  21. }
  22. },
  23. "deprecated": false
  24. }
 

这是一个更强大的例子,加入 <remarks /> 那里面的内容可以是文字或添加的 JSON 或 XML 对象的方法为进一步描述方法文档而服务。

复制代码
  1. /// <summary>
  2. /// Creates a TodoItem.
  3. /// </summary>
  4. /// <remarks>
  5. /// Note that the key is a GUID and not an integer.
  6. ///
  7. /// POST /Todo
  8. /// {
  9. /// "key": "0e7ad584-7788-4ab1-95a6-ca0a5b444cbb",
  10. /// "name": "Item1",
  11. /// "isComplete": true
  12. /// }
  13. ///
  14. /// </remarks>
  15. /// <param name="item"></param>
  16. /// <returns>New Created Todo Item</returns>
  17. /// <response code="201">Returns the newly created item</response>
  18. /// <response code="400">If the item is null</response>
  19. [HttpPost]
  20. [ProducesResponseType(typeof(TodoItem), 201)]
  21. [ProducesResponseType(typeof(TodoItem), 400)]
  22. public IActionResult Create([FromBody, Required] TodoItem item)
  23. {
  24. if (item == null)
  25. {
  26. return BadRequest();
  27. }
  28. TodoItems.Add(item);
  29. return CreatedAtRoute("GetTodo", new { id = item.Key }, item);
  30. }
 

请注意下面是这些附加注释的在用户界面的增强效果。

 

DataAnnotations

你可以使用 System.ComponentModel.DataAnnotations 来标注 API controller ,帮助驱动 Swagger UI 组件。

在 TodoItem 类的 Name 属性上添加 [Required] 标注会改变 UI 中的模型架构信息。[Produces("application/json"] ,正则表达式验证器将更进一步细化生成页面传递的详细信息。代码中使用的元数据信息越多 API 帮助页面上的描述信息也会越多。

复制代码
  1. using System;
  2. using System.ComponentModel;
  3. using System.ComponentModel.DataAnnotations;
  4. namespace TodoApi.Models
  5. {
  6. public class TodoItem
  7. {
  8. public string Key { get; set; }
  9. [Required]
  10. public string Name { get; set; }
  11. [DefaultValue(false)]
  12. public bool IsComplete { get; set; }
  13. }
  14. }
 
 

描述响应类型

API 使用开发者最关心的东西是的返回结果;具体响应类型,错误代码(如果不是标准错误码)。这些都在 XML 注释 和 DataAnnotations 中处理。

以 Create() 方法为例,目前它仅仅返回 “201 Created” 默认响应。如果数据实际创建了或者 POST 正文没有传递数据返回 “204 No Content” 错误,这是理所当然的。但是,如果没有文档知道它的存在或者存在任何其他响应,则可以通过添加下面的代码段是修复这个问题。

复制代码
  1. /// <summary>
  2. /// Creates a TodoItem.
  3. /// </summary>
  4. /// <remarks>
  5. /// Note that the key is a GUID and not an integer.
  6. ///
  7. /// POST /Todo
  8. /// {
  9. /// "key": "0e7ad584-7788-4ab1-95a6-ca0a5b444cbb",
  10. /// "name": "Item1",
  11. /// "isComplete": true
  12. /// }
  13. ///
  14. /// </remarks>
  15. /// <param name="item"></param>
  16. /// <returns>New Created Todo Item</returns>
  17. /// <response code="201">Returns the newly created item</response>
  18. /// <response code="400">If the item is null</response>
  19. [HttpPost]
  20. [ProducesResponseType(typeof(TodoItem), 201)]
  21. [ProducesResponseType(typeof(TodoItem), 400)]
  22. public IActionResult Create([FromBody, Required] TodoItem item)
  23. {
  24. if (item == null)
  25. {
  26. return BadRequest();
  27. }
  28. TodoItems.Add(item);
  29. return CreatedAtRoute("GetTodo", new { id = item.Key }, item);
  30. }
 

 

自定义 UI

stock UI 是一个非常实用的展示方案,如果你想在生成 API 文档页面的时候想把你的标题做的更好看点。

完成与 Swashbuckle 组件相关的任务很简单,但服务需要添加的资源来通常不会被包含在 Web API 项目中,所以必须建立对应的的文件夹结构来承载这些静态资源文件。

在项目中添加 "Microsoft.AspNetCore.StaticFiles": "1.0.0-*" NuGet 包。

在中间件中启用 static files 服务。

复制代码
  1. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  2. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  3. {
  4. app.UseStaticFiles();
  5. app.UseMvcWithDefaultRoute();
  6. // Enable middleware to serve generated Swagger as a JSON endpoint
  7. app.UseSwagger();
  8. // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
  9. app.UseSwaggerUi();
  10. }
 

从 Github repository 上获取 Swagger UI 页面所需的 index.html 核心文件,把他放到 wwwroot/swagger/ui 目录下,并在在同一个文件夹创建一个新的 custom.css 文件。

在 index.html 文件中引用 custom.css 。

复制代码
  1. <link href='custom.css' media='screen' rel='stylesheet' type='text/css' />
 

下面的 CSS 提供了一个自定义页面标题的简单的示例。

custom.css 文件

复制代码
  1. .swagger-section #header
  2. {
  3. border-bottom: 1px solid #000000;
  4. font-style: normal;
  5. font-weight: 400;
  6. font-family: "Segoe UI Light","Segoe WP Light","Segoe UI","Segoe WP",Tahoma,Arial,sans-serif;
  7. background-color: black;
  8. }
  9. .swagger-section #header h1
  10. {
  11. text-align: center;
  12. font-size: 20px;
  13. color: white;
  14. }
 

index.html 正文

复制代码
  1. <body class="swagger-section">
  2. <div id="header">
  3. <h1>ToDo API Documentation</h1>
  4. </div>
  5. <div id="message-bar" class="swagger-ui-wrap" data-sw-translate>&nbsp;</div>
  6. <div id="swagger-ui-container" class="swagger-ui-wrap"></div>
  7. </body>
 

你可以在这个页面有更多改进的东西,请在 Swagger UI Github repository 参阅完整的 UI 资源。

返回目录

由于水平有限,错漏之处在所难免,欢迎大家批评指正,不胜感激,我们将及时修正。
dotNet Core Studying Group:436035237

Swagger 生成 ASP.NET Web API的更多相关文章

  1. ASP.NET Core 中文文档 第二章 指南 (09) 使用 Swagger 生成 ASP.NET Web API 在线帮助测试文档

    原文:ASP.NET Web API Help Pages using Swagger 作者:Shayne Boyer 翻译:谢炀(kiler) 翻译:许登洋(Seay) 对于开发人员来说,构建一个消 ...

  2. ASP.NET Web API 中使用 swagger 来管理 API 文档

    本文以 ASP.NET Web API 为后台框架,利用 EF6 连接 postgreSQL 数据库,使用 swagger 来生成 REST APIs文档.文章分二个部分,第一部分主要讲如何用 EF6 ...

  3. ASP.NET Web API 使用Swagger

    ASP.NET Web API 使用Swagger使用笔记   最近换了工作,其中Webapi这块没有文档,之前有了解过Swagger借此机会好好整理下常用的地方分享给有需要的小伙伴. 概述: 1.s ...

  4. ASP.NET Web API从注释生成帮助文档

    默认情况下,ASP.NET Web API不从Controller的注释中生成帮助文档.如果要将注释作为Web API帮助文档的一部分,比如在帮助文档的Description栏目中显示方法注释中的su ...

  5. ASP.NET Web API 文件產生器 - 使用 Swagger

    转帖:http://kevintsengtw.blogspot.hk/2015/12/aspnet-web-api-swagger.html Swagger 是一套 API 互動文件產生器,使用 HT ...

  6. Asp.Net Web Api中使用Swagger

    关于swagger 设计是API开发的基础.Swagger使API设计变得轻而易举,为开发人员.架构师和产品所有者提供了易于使用的工具. 官方网址:https://swagger.io/solutio ...

  7. ASP.NET Web API根据代码注释生成Help文档

    使用Visual Studio新建一个ASP.NET Web API项目,直接运行,查看Help文档可以看到如下的API帮助说明 如何在Description中显示描述. 1. 打开Controlle ...

  8. 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用

    由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...

  9. ASP.NET Web API Model-ActionBinding

    ASP.NET Web API Model-ActionBinding 前言 前面的几个篇幅把Model部分的知识点划分成一个个的模块来讲解,而在控制器执行过程中分为好多个过程,对于控制器执行过程(一 ...

随机推荐

  1. 【动态规划】XMU 1029 矩阵链乘法

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1029 题目大意: 题同乘法难题.给n+1个数,头尾不能动,中间的数可取出,取出时代价是 ...

  2. http 400 错误的请求怎么解决

    HTTP400是个错误的统称 你将IE选项-高级中的显示HTTP友好错误信息前面的勾去掉. 然后在开这个页,,把错误代码复制出来 .其实有时是网页本身已经不可用了,你先关掉浏览器稍等一会再登陆网页也是 ...

  3. Delphi——Window 消息 - 转载▼

    Delphi是Borland公司的一种面向对象的可视化软件开发工具. Delphi集中了Visual C++和Visual Basic两者的优点:容易上手.功能强大,特别是在界面设计.数据库编程.网络 ...

  4. centos 64bit 安装与升级 chrome的方法

    vim /etc/yum.repos.d/CentOS-Base.repo 根据你的系统增加一个节点 [google64] name=Google - x86_64 baseurl=http://dl ...

  5. ajax提交数据问题

    加入traditional:true属性 traditional 类型:Boolean 如果你想要用传统的方式来序列化数据,那么就设置为 true.请参考工具分类下面的 jQuery.param 方法 ...

  6. windows2012R2虚拟机快速激活的方法

    相信Hyper-v管理员都有这样的经历,安装多台虚拟机后,都要一台一台手工激活,如果虚拟机足够多的话,这是一项很繁琐的工作,但从Windows Server 2012 R2开始,就不需要这么做了,微软 ...

  7. Python标准库:内置函数bytearray([source[, encoding[, errors]]])

    返回一个新字节数组.这个数组里的元素是可变的.而且每一个元素的值范围: 0 <= x < 256.能够通过"字节与字节数组操作"章节来查看相关字节数组的内容.以下说明一 ...

  8. yii 自动生成的内容,分页信息(Displaying 1-10 of 15 results.)如何修改或是yii cgridview template summary

    问的白一点就是 Displaying 1-10 of 15 results 如何翻译 如果搜索的话, 搜这个会出来很多内容 yii cgridview template summary 好了,其他不说 ...

  9. LA 6449 IQ Test

    [题目] 给出一个长度为n的数组(8<n<12),告诉你规律  (1<=d<=3)要求d尽量小 现在求第n+1项 [题解] 水题 不知道怎么求a1~ad? 用克拉默法则 [代码 ...

  10. eclipse 库 library jar包 工程 总结

    引用库错误 如果在libraries中发现有小红叉,表明引用库错误 解决办法:在左侧projects中add引用到的库 如:我们的支付库引用了以下三个库 那么需要在projects中add这三个库   ...