其他分享
首页 > 其他分享> > Swagger生成webapi文档

Swagger生成webapi文档

作者:互联网

WebApi接口开发完毕后,交付给前端人员或手机端开发者时接口说明文档是必不可少的配套设备,如果公司流程不规范大家使用口口相传的交接方式,而且没有改进的欲望,那你可以到此为止了。Swagger是方便测试接口,快速展示注释内容,生成Restful风格接口文档的框架。

Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因:

Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API。
Swagger 可以生成客户端SDK代码用于各种不同的平台上的实现。
Swagger 文件可以在许多不同的平台上从代码注释中自动生成。
Swagger 有一个强大的社区,里面有许多强悍的贡献者。

1、安装Swashbuckle

2、初次运行 输入 自己网站地址/swagger 我这里是http://www.permissionapi.com/swagger

3、添加自定义ApiController

  1. public class AdminController : ApiController
  2. {
  3. /// <summary>
  4. /// 获取管理员信息
  5. /// </summary>
  6. /// <param name="username">管理员姓名</param>
  7. /// <param name="pwd">管理员密码</param>
  8. /// <returns></returns>
  9. public string Get(string username,string pwd)
  10. {
  11. return $"username:{username},pwd:{pwd}";
  12. }
  13. }

 

4、添加实现了ISwaggerProvider接口的类

在App_Start文件夹中添加SwaggerControllerDescProvider.cs,相关代码如下:

  1. namespace WebApiSwagger.Main.App_Start
  2. {
  3. /// <summary>
  4. /// 显示swagger控制器的描述
  5. /// </summary>
  6. public class SwaggerControllerDescProvider : ISwaggerProvider
  7. {
  8. private readonly ISwaggerProvider _swaggerProvider;
  9. private static ConcurrentDictionary<string, SwaggerDocument> _cache = new ConcurrentDictionary<string, SwaggerDocument>();
  10. private readonly string _xml;
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. /// <param name="swaggerProvider"></param>
  15. /// <param name="xml">xml文档路径</param>
  16. public SwaggerControllerDescProvider(ISwaggerProvider swaggerProvider, string xml)
  17. {
  18. _swaggerProvider = swaggerProvider;
  19. _xml = xml;
  20. }
  21. public SwaggerDocument GetSwagger(string rootUrl, string apiVersion)
  22. {
  23. var cacheKey = string.Format("{0}_{1}", rootUrl, apiVersion);
  24. SwaggerDocument srcDoc = null;
  25. //只读取一次
  26. if (!_cache.TryGetValue(cacheKey, out srcDoc))
  27. {
  28. srcDoc = _swaggerProvider.GetSwagger(rootUrl, apiVersion);
  29. srcDoc.vendorExtensions = new Dictionary<string, object> { { "ControllerDesc", GetControllerDesc() } };
  30. _cache.TryAdd(cacheKey, srcDoc);
  31. }
  32. return srcDoc;
  33. }
  34. /// <summary>
  35. /// 从API文档中读取控制器描述
  36. /// </summary>
  37. /// <returns>所有控制器描述</returns>
  38. public ConcurrentDictionary<string, string> GetControllerDesc()
  39. {
  40. string xmlpath = _xml;
  41. ConcurrentDictionary<string, string> controllerDescDict = new ConcurrentDictionary<string, string>();
  42. if (File.Exists(xmlpath))
  43. {
  44. XmlDocument xmldoc = new XmlDocument();
  45. xmldoc.Load(xmlpath);
  46. string type = string.Empty, path = string.Empty, controllerName = string.Empty;
  47. string[] arrPath;
  48. int length = -1, cCount = "Controller".Length;
  49. XmlNode summaryNode = null;
  50. foreach (XmlNode node in xmldoc.SelectNodes("//member"))
  51. {
  52. type = node.Attributes["name"].Value;
  53. if (type.StartsWith("T:"))
  54. {
  55. //控制器
  56. arrPath = type.Split('.');
  57. length = arrPath.Length;
  58. controllerName = arrPath[length - 1];
  59. if (controllerName.EndsWith("Controller"))
  60. {
  61. //获取控制器注释
  62. summaryNode = node.SelectSingleNode("summary");
  63. string key = controllerName.Remove(controllerName.Length - cCount, cCount);
  64. if (summaryNode != null && !string.IsNullOrEmpty(summaryNode.InnerText) && !controllerDescDict.ContainsKey(key))
  65. {
  66. controllerDescDict.TryAdd(key, summaryNode.InnerText.Trim());
  67. }
  68. }
  69. }
  70. }
  71. }
  72. return controllerDescDict;
  73. }
  74. }
  75. }

5、添加功能性js文件

在Scripts文件夹中添加SwaggerConfig.js脚本文件,将其设置为“嵌入的资源”。这个js文件的功能主要有两个:一个是汉化,另一个是在界面上显示控制器的描述文字。

代码如下:

  1. 'use strict';
  2. window.SwaggerTranslator = {
  3. _words: [],
  4. translate: function () {
  5. var $this = this;
  6. $('[data-sw-translate]').each(function () {
  7. $(this).html($this._tryTranslate($(this).html()));
  8. $(this).val($this._tryTranslate($(this).val()));
  9. $(this).attr('title', $this._tryTranslate($(this).attr('title')));
  10. });
  11. },
  12. setControllerSummary: function () {
  13. $.ajax({
  14. type: "get",
  15. async: true,
  16. url: $("#input_baseUrl").val(),
  17. dataType: "json",
  18. success: function (data) {
  19. var summaryDict = data.ControllerDesc;
  20. var id, controllerName, strSummary;
  21. $("#resources_container .resource").each(function (i, item) {
  22. id = $(item).attr("id");
  23. if (id) {
  24. controllerName = id.substring(9);
  25. strSummary = summaryDict[controllerName];
  26. if (strSummary) {
  27. $(item).children(".heading").children(".options").first().prepend('<li class="controller-summary" title="' + strSummary + '">' + strSummary + '</li>');
  28. }
  29. }
  30. });
  31. }
  32. });
  33. },
  34. _tryTranslate: function (word) {
  35. return this._words[$.trim(word)] !== undefined ? this._words[$.trim(word)] : word;
  36. },
  37. learn: function (wordsMap) {
  38. this._words = wordsMap;
  39. }
  40. };
  41. /* jshint quotmark: double */
  42. window.SwaggerTranslator.learn({
  43. "Warning: Deprecated": "警告:已过时",
  44. "Implementation Notes": "实现备注",
  45. "Response Class": "响应类",
  46. "Status": "状态",
  47. "Parameters": "参数",
  48. "Parameter": "参数",
  49. "Value": "值",
  50. "Description": "描述",
  51. "Parameter Type": "参数类型",
  52. "Data Type": "数据类型",
  53. "Response Messages": "响应消息",
  54. "HTTP Status Code": "HTTP状态码",
  55. "Reason": "原因",
  56. "Response Model": "响应模型",
  57. "Request URL": "请求URL",
  58. "Response Body": "响应体",
  59. "Response Code": "响应码",
  60. "Response Headers": "响应头",
  61. "Hide Response": "隐藏响应",
  62. "Headers": "头",
  63. "Try it out!": "试一下!",
  64. "Show/Hide": "显示/隐藏",
  65. "List Operations": "显示操作",
  66. "Expand Operations": "展开操作",
  67. "Raw": "原始",
  68. "can't parse JSON. Raw result": "无法解析JSON. 原始结果",
  69. "Model Schema": "模型架构",
  70. "Model": "模型",
  71. "apply": "应用",
  72. "Username": "用户名",
  73. "Password": "密码",
  74. "Terms of service": "服务条款",
  75. "Created by": "创建者",
  76. "See more at": "查看更多:",
  77. "Contact the developer": "联系开发者",
  78. "api version": "api版本",
  79. "Response Content Type": "响应Content Type",
  80. "fetching resource": "正在获取资源",
  81. "fetching resource list": "正在获取资源列表",
  82. "Explore": "浏览",
  83. "Show Swagger Petstore Example Apis": "显示 Swagger Petstore 示例 Apis",
  84. "Can't read from server. It may not have the appropriate access-control-origin settings.": "无法从服务器读取。可能没有正确设置access-control-origin。",
  85. "Please specify the protocol for": "请指定协议:",
  86. "Can't read swagger JSON from": "无法读取swagger JSON于",
  87. "Finished Loading Resource Information. Rendering Swagger UI": "已加载资源信息。正在渲染Swagger UI",
  88. "Unable to read api": "无法读取api",
  89. "from path": "从路径",
  90. "server returned": "服务器返回"
  91. });
  92. $(function () {
  93. window.SwaggerTranslator.translate();
  94. window.SwaggerTranslator.setControllerSummary();
  95. });

6、修改SwaggerConfig.cs

Swagger安装完毕后App_Start文件夹下才会有SwaggerConfig.cs文件,修改后的SwaggerConfig.cs的代码如下:

注意代码中 JiaHua.Permission.Api是项目名称,请改成自己项目对应的名称!

  1. public class SwaggerConfig
  2. {
  3. public static void Register()
  4. {
  5. var thisAssembly = typeof(SwaggerConfig).Assembly;
  6. GlobalConfiguration.Configuration
  7. .EnableSwagger(c =>
  8. {
  9. c.SingleApiVersion("v1", "JiaHua.Permission.Api");
  10. //添加下述代码
  11. var xmlFile = string.Format("{0}/bin/JiaHua.Permission.Api.XML", System.AppDomain.CurrentDomain.BaseDirectory);
  12. if (System.IO.File.Exists(xmlFile))
  13. {
  14. c.IncludeXmlComments(xmlFile);
  15. }
  16. c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
  17. c.CustomProvider((defaultProvider) => new SwaggerControllerDescProvider(defaultProvider, xmlFile));
  18. })
  19. .EnableSwaggerUi(b =>
  20. {
  21. b.InjectJavaScript(Assembly.GetExecutingAssembly(), "JiaHua.Permission.Api.Scripts.SwaggerConfig.js");
  22. });
  23. }
  24. }

7、修改项目的“XML文档文件”属性

右键项目--》属性--》生成

8、注释显示成功

9、小瑕疵及解决方法

 

み旋律 发布了20 篇原创文章 · 获赞 0 · 访问量 777 私信 关注

标签:webapi,function,string,public,文档,controllerName,Swagger,Response
来源: https://blog.csdn.net/weixin_44481764/article/details/104503075