| | |
| | | /// <summary> |
| | | /// Swagger 显示控制器的描述 |
| | | /// </summary> |
| | | public class SwaggerCacheProvider : ISwaggerProvider |
| | | { |
| | | public class SwaggerCacheProvider : ISwaggerProvider { |
| | | private readonly ISwaggerProvider _swaggerProvider; |
| | | private static ConcurrentDictionary<string, SwaggerDocument> _cache = new ConcurrentDictionary<string, SwaggerDocument>(); |
| | | private readonly string _xmlPath; |
| | |
| | | /// <summary></summary> |
| | | /// <param name="swaggerProvider"></param> |
| | | /// <param name="xmlpath">xml文档路径</param> |
| | | public SwaggerCacheProvider(ISwaggerProvider swaggerProvider, string xmlpath) |
| | | { |
| | | public SwaggerCacheProvider(ISwaggerProvider swaggerProvider, string xmlpath) { |
| | | _swaggerProvider = swaggerProvider; |
| | | _xmlPath = xmlpath; |
| | | } |
| | | |
| | | public SwaggerDocument GetSwagger(string rootUrl, string apiVersion) |
| | | { |
| | | public SwaggerDocument GetSwagger(string rootUrl, string apiVersion) { |
| | | var cacheKey = string.Format("{0}_{1}", rootUrl, apiVersion); |
| | | // 只读取一次 |
| | | if (!_cache.TryGetValue(cacheKey, out SwaggerDocument srcDoc)) |
| | | { |
| | | if (!_cache.TryGetValue(cacheKey, out SwaggerDocument srcDoc)) { |
| | | srcDoc = _swaggerProvider.GetSwagger(rootUrl, apiVersion); |
| | | |
| | | srcDoc.vendorExtensions = new Dictionary<string, object> { |
| | |
| | | /// 从API文档中读取控制器描述 |
| | | /// </summary> |
| | | /// <returns>所有控制器描述</returns> |
| | | public ConcurrentDictionary<string, string> GetControllerDesc() |
| | | { |
| | | public ConcurrentDictionary<string, string> GetControllerDesc() { |
| | | ConcurrentDictionary<string, string> controllerDescDict = new ConcurrentDictionary<string, string>(); |
| | | if (File.Exists(_xmlPath)) |
| | | { |
| | | if (File.Exists(_xmlPath)) { |
| | | XmlDocument xmldoc = new XmlDocument(); |
| | | xmldoc.Load(_xmlPath); |
| | | |
| | | string[] arrPath; |
| | | int cCount = "Controller".Length; |
| | | foreach (XmlNode node in xmldoc.SelectNodes("//member")) |
| | | { |
| | | foreach (XmlNode node in xmldoc.SelectNodes("//member")) { |
| | | string type = node.Attributes["name"].Value; |
| | | if (type.StartsWith("T:")) |
| | | { |
| | | if (type.StartsWith("T:")) { |
| | | arrPath = type.Split('.'); |
| | | string controllerName = arrPath[arrPath.Length - 1]; |
| | | if (controllerName.EndsWith("Controller")) // 控制器 |
| | |
| | | // 获取控制器注释 |
| | | XmlNode summaryNode = node.SelectSingleNode("summary"); |
| | | string key = controllerName.Remove(controllerName.Length - cCount, cCount); |
| | | if (summaryNode != null && !string.IsNullOrEmpty(summaryNode.InnerText) && !controllerDescDict.ContainsKey(key)) |
| | | { |
| | | if (summaryNode != null && !string.IsNullOrEmpty(summaryNode.InnerText) && !controllerDescDict.ContainsKey(key)) { |
| | | controllerDescDict.TryAdd(key, summaryNode.InnerText.Trim()); |
| | | } |
| | | } |