参考文章: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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)