Asp.Net Web API项目配置Miniprofiler和Swagger集成
-
新建Web API项目
-
配置Swagger,本项目中使用的swagger包如下
-
Nuget安装MiniProfiler,目前最新版本是4.2.1,支持的.Net框架是4.6.1,注意不要选错
-
在Global.asax.cs中添加如下代码
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
MiniProfiler.Configure(new MiniProfilerOptions
{
RouteBasePath = "~/mini-profiler-resources",
PopupRenderPosition = RenderPosition.Right, // defaults to left
PopupMaxTracesToShow = 2, // defaults to 15
ColorScheme = ColorScheme.Auto, // defaults to light
IgnoredPaths = { "/lib/","/css/","images"}
});
}
protected void Application_BeginRequest()
{
MiniProfiler.StartNew();
}
protected void Application_EndRequest()
{
MiniProfiler.Current?.Stop();
}
- web.config当中添加如下代码(添加在节点下)
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*"
type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified"
preCondition="integratedMode" />
- 这时候在swagger当中调用API之后,再访问~/mini-profiler-resources/results路径就会看到如下结果
如何才能使监控结果直接显示在Swagger页面呢?别急,继续如下步骤
- 下载默认的index.html页面添加到项目当中,注意:需要将index.html设置为嵌入的资源 官方下载