2014-03-29 54 views
13

編輯 得到的答案here使用MiniProfiler與MVC 5

所以我想退房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(); 

添加這一行jquery的束下面_layout:

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

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

然該應用。 什麼都沒有顯示。沒有分析,沒有。

我錯過了什麼?

問候。

回答

5

還必須添加呼叫:

MiniProfiler.Start(); 

在Global.asax.cs中來的Application_BeginRequest事件。

和:

MiniProfiler.Stop(); 

在Global.asax.cs中,以Application_EndRequest事件。

27

這是我必須做的就是MiniProfiler我ASP.NET MVC5項目的工作:

  1. 安裝了MiniProfilerMiniProfiler.MVC4的NuGet包(該MVC4包支持MVC5)

  2. 以下內容添加到Application_Start()在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. 以下添加到 '的Application_BeginRequest()' 和 'Application_EndRequest()',也是在Global.asax中:

    protected void Application_BeginRequest() 
    { 
        if (Request.IsLocal) 
        { 
         MiniProfiler.Start(); 
        } 
    } 
    
    protected void Application_EndRequest() 
    { 
        MiniProfiler.Stop(); 
    } 
    
  4. (只是</body>標記之前)添加以下_Layout.cshtml:

    ... 
        @StackExchange.Profiling.MiniProfiler.RenderIncludes() 
    </body> 
    </html> 
    
  5. 以下內容添加到Web.config文件的<handlers>部分:

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

這足以描述每個MVC控制器操作和視圖。


在我的特別項目中,我使用實體框架6,所以我也做了以下內容:

一)安裝了MiniProfiler.EF6

二)增加了以下的Global.asax中Application_Start()的結尾:

... 
    MiniProfilerEF6.Initialize(); 
} 
+0

太棒了,我沒有意識到自動控制器和視圖分析的可能性 – Squazz