Using MiniProfiler with MVC 5

筅森魡賤 提交于 2019-11-30 08:42:05

This is what I had to do to get MiniProfiler working in my ASP.NET MVC5 project:

  1. Installed the MiniProfiler and MiniProfiler.MVC4 NuGet packages (the MVC4 package supports MVC5)

  2. Add the following to Application_Start() in Global.asax:

    protected void Application_Start()
    {
        ...
        // Setup profiler for Controllers via a Global ActionFilter
        GlobalFilters.Filters.Add(new ProfilingActionFilter());
    
        // initialize automatic view profiling
        var copy = ViewEngines.Engines.ToList();
        ViewEngines.Engines.Clear();
        foreach (var item in copy)
        {
            ViewEngines.Engines.Add(new ProfilingViewEngine(item));
        }
    }
    
  3. Add the following to 'Application_BeginRequest()' and 'Application_EndRequest()', also in Global.asax:

    protected void Application_BeginRequest()
    {
        if (Request.IsLocal)
        {
            MiniProfiler.Start();
        }
    }
    
    protected void Application_EndRequest()
    {
        MiniProfiler.Stop();
    }
    
  4. Add the following to _Layout.cshtml (just before the </body> tag):

        ...
        @StackExchange.Profiling.MiniProfiler.RenderIncludes()
    </body>
    </html>
    
  5. Add the following to the <handlers> section of Web.config:

    <system.webServer>
        ...
        <handlers>
            ...
            <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*"
                 type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified"
                 preCondition="integratedMode" />
            ...
        </handlers>
    </system.webServer>
    

That was enough to profile each of the MVC Controller Actions and Views.


In my particular project I was using Entity Framework 6, so I also did the following:

a) Installed the MiniProfiler.EF6 package

b) Added the following to the end of Application_Start() in Global.asax:

    ...
    MiniProfilerEF6.Initialize();
}

Also you have to add call:

MiniProfiler.Start();

In Global.asax.cs to Application_BeginRequest event.

And:

MiniProfiler.Stop();

In Global.asax.cs to Application_EndRequest event.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!