123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Web.Http;
- using System.Web.Http.Filters;
- using System.Web.Http.Routing;
- using System.Xml;
- using Microsoft.Owin;
- using Microsoft.Owin.Cors;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Serialization;
- using Owin;
- using Swashbuckle.Application;
- using Swashbuckle.Swagger;
- using XdCxRhDW.App.WebAPI;
- [assembly: OwinStartup(typeof(XdCxRhDW.App.WebAPI.Startup))]
- namespace XdCxRhDW.App.WebAPI
- {
- class Startup
- {
- public void Configuration(IAppBuilder app)
- {
- HttpConfiguration config = new HttpConfiguration();
- JsonSerializerSettings setting = new JsonSerializerSettings()
- {
- //日期类型默认格式化处理
- DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
- DateFormatString = "yyyy-MM-dd HH:mm:ss",
- //驼峰样式
- ContractResolver = new CamelCasePropertyNamesContractResolver(),
- //空值处理
- //NullValueHandling = NullValueHandling.Ignore,
- //设置序列化的最大层数
- MaxDepth = 10,
- //解决json序列化时的循环引用问题
- ReferenceLoopHandling = ReferenceLoopHandling.Ignore
- };
- config.Formatters.JsonFormatter.SerializerSettings = setting;
- config.Formatters.Remove(config.Formatters.XmlFormatter);
- config.Filters.Add(new HandlerErrorAttribute());
- //config.Routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" });
- config.Routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}");
- //config.Routes.MapHttpRoute("DefaultApiGet", "Api/{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
- //config.Routes.MapHttpRoute("DefaultApiPost", "Api/{controller}", new { action = "Post" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });
- ConfigureSwagger(config);
- //添加路由路径
- config.MapHttpAttributeRoutes();
- app.UseCors(CorsOptions.AllowAll);
- //app.Use<LoggingMiddleware>();
- app.UseWebApi(config);
- }
- private static void ConfigureSwagger(HttpConfiguration config)
- {
- var thisAssembly = typeof(Startup).Assembly;
- config.EnableSwagger(c =>
- {
- c.SingleApiVersion("v1", "多模式融合定位平台Http接口");
- //设置接口描述xml路径地址
- var webApiXmlPath1 = $"{AppDomain.CurrentDomain.BaseDirectory}{Path.GetFileNameWithoutExtension(System.AppDomain.CurrentDomain.FriendlyName)}.xml";
- c.IncludeXmlComments(webApiXmlPath1);
- var webApiXmlPath2 = $"{AppDomain.CurrentDomain.BaseDirectory}XdCxRhDw.Dto.xml";
- c.IncludeXmlComments(webApiXmlPath2);
- c.UseFullTypeNameInSchemaIds();
- //加入控制器描述
- c.CustomProvider((defaultProvider) => new SwaggerControllerDescProvider(defaultProvider, webApiXmlPath1));
- })
- .EnableSwaggerUi(c =>
- {
- c.InjectJavaScript(thisAssembly, "XdCxRhDW.App.WebAPI.Swagger.js");
- c.DocumentTitle("");
- });
- }
- public class HandlerErrorAttribute : ExceptionFilterAttribute
- {
- /// <summary>
- /// 控制器方法中出现异常,会调用该方法捕获异常
- /// </summary>
- /// <param name="context">提供使用</param>
- public override void OnException(HttpActionExecutedContext context)
- {
- base.OnException(context);
- Serilog.Log.Error(context.Exception, context.Exception.Message);
- //LogFile.WriteError(context.Exception.Message);
- throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.OK)
- {
- Content = new StringContent(
- JsonConvert.SerializeObject(
- new
- {
- code = -1,
- data = "",
- msg = context.Exception.Message
- }), Encoding.UTF8, "text/json")
- });
- }
- };
- public class SwaggerControllerDescProvider : ISwaggerProvider
- {
- private readonly ISwaggerProvider _swaggerProvider;
- private static ConcurrentDictionary<string, SwaggerDocument> _cache = new ConcurrentDictionary<string, SwaggerDocument>();
- private readonly string _xml;
- /// <summary>
- ///
- /// </summary>
- /// <param name="swaggerProvider"></param>
- /// <param name="xml">xml文档路径</param>
- public SwaggerControllerDescProvider(ISwaggerProvider swaggerProvider, string xml)
- {
- _swaggerProvider = swaggerProvider;
- _xml = xml;
- }
- 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 ConcurrentDictionary<string, string> GetControllerDesc()
- {
- string xmlpath = _xml;
- 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;
- }
- }
- }
- }
|