Swagger(webapi自动生成接口说明文档)

时间:2022-06-28 05:46:41

标签:

1.引入Swagger.Net.UI和Swashbuckle包 2.卸载重复包Swagger.Net

3.多余的SwaggerUI文件夹

4.项目属性->勾选生成xml文档文件

5.添加类SwaggerCachingProvider和修改SwaggerConfig文件

using Swashbuckle.Swagger; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Xml; namespace Swagger.App_Start { public class SwaggerCachingProvider : ISwaggerProvider { private static ConcurrentDictionary<string, SwaggerDocument> _cache = new ConcurrentDictionary<string, SwaggerDocument>(); private readonly ISwaggerProvider _swaggerProvider; public SwaggerCachingProvider(ISwaggerProvider swaggerProvider) { _swaggerProvider = swaggerProvider; } public SwaggerDocument GetSwagger(string rootUrl, string apiVersion) { var cacheKey = string.Format("{0}_{1}", rootUrl, apiVersion); SwaggerDocument srcDoc = null; //只读取一次 if (!_cache.TryGetValue(cacheKey, out srcDoc)) { srcDoc = _swaggerProvider.GetSwagger(rootUrl, apiVersion); srcDoc.vendorExtensions = new Dictionary<string, object> { { "ControllerDesc", GetControllerDesc() } }; _cache.TryAdd(cacheKey, srcDoc); } return srcDoc; } /// <summary> /// 从API文档中读取控制器描述 /// </summary> /// <returns>所有控制器描述</returns> public static ConcurrentDictionary<string, string> GetControllerDesc() { string xmlpath = string.Format("{0}/App_Data/SuiBao.WebApi.XML", System.AppDomain.CurrentDomain.BaseDirectory); ConcurrentDictionary<string, string> controllerDescDict = new ConcurrentDictionary<string, string>(); if (File.Exists(xmlpath)) { XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(xmlpath); string type = string.Empty, path = string.Empty, controllerName = string.Empty; string[] arrPath; int length = -1, cCount = "Controller".Length; XmlNode summaryNode = null; foreach (XmlNode node in xmldoc.SelectNodes("//member")) { type = node.Attributes["name"].Value; if (type.StartsWith("T:")) { //控制器 arrPath = type.Split(.); length = arrPath.Length; controllerName = arrPath[length - 1]; if (controllerName.EndsWith("Controller")) { //获取控制器注释 summaryNode = node.SelectSingleNode("summary"); string key = controllerName.Remove(controllerName.Length - cCount, cCount); if (summaryNode != null && !string.IsNullOrEmpty(summaryNode.InnerText) && !controllerDescDict.ContainsKey(key)) { controllerDescDict.TryAdd(key, summaryNode.InnerText.Trim()); } } } } } return controllerDescDict; } } }

using System.Web.Http; using WebActivatorEx; using Swagger; using Swashbuckle.Application; using Swagger.App_Start; [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] namespace Swagger { public class SwaggerConfig { public static void Register() { var thisAssembly = typeof(SwaggerConfig).Assembly; GlobalConfiguration.Configuration .EnableSwagger(c => { c.SingleApiVersion("v1", "Swagger"); c.IncludeXmlComments(string.Format("{0}/App_Data/SuiBao.WebApi.XML", System.AppDomain.CurrentDomain.BaseDirectory)); c.CustomProvider((defaultProvider) => new SwaggerCachingProvider(defaultProvider)); }) .EnableSwaggerUi(c => { c.InjectJavaScript(thisAssembly, "Swagger.Scripts.swagger_lang.js"); }); } } }

6.汉化

1.添加js文件