128 lines
4.6 KiB
C#
128 lines
4.6 KiB
C#
using System.Collections.Concurrent;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Xml;
|
||
using System.Linq;
|
||
using System.Web.Http;
|
||
using WebActivatorEx;
|
||
using Swashbuckle.Application;
|
||
using Swashbuckle.Swagger;
|
||
using GSYWApi;
|
||
|
||
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
|
||
|
||
namespace GSYWApi
|
||
{
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public class SwaggerConfig
|
||
{
|
||
/// <summary>
|
||
/// ×¢²áʼþ
|
||
/// </summary>
|
||
public static void Register()
|
||
{
|
||
var thisAssembly = typeof(SwaggerConfig).Assembly;
|
||
|
||
GlobalConfiguration.Configuration
|
||
.EnableSwagger(c =>
|
||
{
|
||
c.SingleApiVersion("v1", "GSYWApi");
|
||
c.IncludeXmlComments(string.Format("{0}/bin/GSYWApi.XML", System.AppDomain.CurrentDomain.BaseDirectory));//ÉèÖÃxmlµØÖ·
|
||
c.CustomProvider((defaultProvider) => new CachingSwaggerProvider(defaultProvider));
|
||
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
|
||
})
|
||
.EnableSwaggerUi(c =>
|
||
{
|
||
c.DocumentTitle("My webapi");
|
||
c.InjectJavaScript(thisAssembly, "GSYWApi.Scripts.Swagger.swagger_lang.js");//ºº»¯js
|
||
});
|
||
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public class CachingSwaggerProvider : ISwaggerProvider
|
||
{
|
||
private static ConcurrentDictionary<string, SwaggerDocument> _cache =
|
||
new ConcurrentDictionary<string, SwaggerDocument>();
|
||
|
||
private readonly ISwaggerProvider _swaggerProvider;
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="swaggerProvider"></param>
|
||
public CachingSwaggerProvider(ISwaggerProvider swaggerProvider)
|
||
{
|
||
_swaggerProvider = swaggerProvider;
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="rootUrl"></param>
|
||
/// <param name="apiVersion"></param>
|
||
/// <returns></returns>
|
||
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}/bin/YFBusinessApi.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;
|
||
}
|
||
}
|
||
}
|