【问题标题】:Using MiniProfiler with MVC 5将 MiniProfiler 与 MVC 5 一起使用
【发布时间】:2014-03-29 05:45:18
【问题描述】:

编辑 得到答案here

所以我想查看 MiniProfiler 来解决一些性能问题。 在将它用于生产代码之前,我想通过示例进行尝试,因此继续创建 MVC 5 应用程序。这是使用模板创建的普通应用程序。

在 HomeController 的 Index() 方法中添加了这段代码:

var profiler = MiniProfiler.Current;
        using (profiler.Step("Set page title"))
        {
            ViewBag.Title = "Home Page";
        }
        using (profiler.Step("Doing complex stuff"))
        {
            using (profiler.Step("Step A"))
            { // something more interesting here
                Thread.Sleep(100);
            }
            using (profiler.Step("Step B"))
            { // and here
                Thread.Sleep(250);
            }
        }
        return View();

在 _Layout 中的 jquery 包下面添加了这一行:

@Scripts.Render("~/bundles/jquery")
@StackExchange.Profiling.MiniProfiler.RenderIncludes()

@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)

运行应用程序。 什么都没有出现。没有分析,什么都没有。

我错过了什么?

问候。

【问题讨论】:

    标签: asp.net-mvc mvc-mini-profiler


    【解决方案1】:

    为了让 MiniProfiler 在我的 ASP.NET MVC5 项目中工作,我必须这样做:

    1. 安装了 MiniProfilerMiniProfiler.MVC4 NuGet 包(MVC4 包支持 MVC5)

    2. 将以下内容添加到 Global.asax 中的Application_Start()

      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. 将以下内容添加到“Application_BeginRequest()”和“Application_EndRequest()”,也在 Global.asax 中:

      protected void Application_BeginRequest()
      {
          if (Request.IsLocal)
          {
              MiniProfiler.Start();
          }
      }
      
      protected void Application_EndRequest()
      {
          MiniProfiler.Stop();
      }
      
    4. 将以下内容添加到 _Layout.cshtml(就在 </body> 标记之前):

          ...
          @StackExchange.Profiling.MiniProfiler.RenderIncludes()
      </body>
      </html>
      
    5. 将以下内容添加到 Web.config 的 &lt;handlers&gt; 部分:

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

    这足以分析每个 MVC 控制器操作和视图。


    在我的特定项目中,我使用的是 Entity Framework 6,因此我还做了以下操作:

    a) 安装了 MiniProfiler.EF6

    b) 在 Global.asax 中 Application_Start() 的末尾添加以下内容:

        ...
        MiniProfilerEF6.Initialize();
    }
    

    【讨论】:

    • 太棒了,我不知道自动控制器和视图分析的可能性
    【解决方案2】:

    你还必须添加调用:

    MiniProfiler.Start();
    

    在 Global.asax.cs 到 Application_BeginRequest 事件。

    还有:

    MiniProfiler.Stop();
    

    在 Global.asax.cs 到 Application_EndRequest 事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      相关资源
      最近更新 更多