参考文章:https://www.cnblogs.com/jiujiduilie/p/8371378.html
1、通过NuGet安装MiniProfiler
2、通过Nuget安装MiniProfiler.EF6
3、通过Nuget安装Swashbuckle
4、添加web.config配置,添加到<handlers>节点下面
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
5、在 Global.asax中添加
protected void Application_BeginRequest() { MiniProfiler.Start(); } protected void Application_EndRequest() { MiniProfiler.Stop(); }
6、添加InjectMiniProfiler类,然后在App_Start/SwaggerConfig.cs中的EnableSwagger中添加 c.DocumentFilter<InjectMiniProfiler>();
public class InjectMiniProfiler : IDocumentFilter { public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer) { swaggerDoc.info.contact = new Contact() { name = MiniProfiler.RenderIncludes().ToHtmlString() }; } }
7、添加SwaggerUiCustomization.js,并且右键该文件修改其属性中的【生成操作】为【嵌入的资源】
//Create a mini profiler script tag with the right properites var MiniProfiler = $('#api_info > div:nth-child(3)').text(); const attributes = [ 'src', 'data-version', 'data-path', 'data-current-id', 'data-ids', 'data-position', 'data-trivial', 'data-children', 'data-max-traces', 'data-controls', 'data-authorized', 'data-toggle-shortcut', 'data-start-hidden', 'data-trivial-milliseconds' ]; var GetAttr = function (input, attributeName) { const myRegexp = attributeName + '="(.*?)"'; const re = new RegExp(myRegexp, "g"); const match = re.exec(input); return match[1]; } var s = document.createElement("script"); s.type = "text/javascript"; s.id = "mini-profiler"; s.async = true; for (var i = 0; i < attributes.length; i++) { var element = attributes[i]; s.setAttribute(element, GetAttr(MiniProfiler, element)); } document.body.appendChild(s); // Remove injected tag from view $('#api_info > div:nth-child(3)').text('');
8、给swaggerconfig.cs添加
string resourceName2 = thisAssembly.FullName.Substring(0, thisAssembly.FullName.IndexOf(",")) + ".Scripts.swaggerui.SwaggerUiCustomization.js"; c.InjectJavaScript(thisAssembly, resourceName2);
9、添加WebApiProfilingActionFilter类
public class WebApiProfilingActionFilter : ActionFilterAttribute { public const string MiniProfilerResultsHeaderName = "X-MiniProfiler-Ids"; public override void OnActionExecuted(HttpActionExecutedContext filterContext) { var MiniProfilerJson = JsonConvert.SerializeObject(new[] {MiniProfiler.Current.Id}); filterContext.Response.Content.Headers.Add(MiniProfilerResultsHeaderName, MiniProfilerJson); } }
10、往SwaggerUiCustomization.js文件中添加代码window.angular=true;
11、在WebApiConfig.cs中注册这个Filterconfig.Filters.Add(new WebApiProfilingActionFilter());
12、将【项目】-【属性】-【生成】-【输出】-XML文档文件 勾上
GitHub:https://github.com/zhuanshujianghai/WebApi_Swagger_Miniprofiler_Demo