2013年1月17日 星期四

ASP.NET MVC 4 使用 MiniProfiler 的調整方式

之前部落格裡介紹 ASP.NET MVC 使用 MiniProfiler 的文章都還是使用 ASP.NET MVC 3,到了 ASP.NET MVC 4 之後就沒有再介紹過了,而 ASP.NET MVC 4 仍然還是可以用 MiniProfiler 整合套件還是使用「MiniProfiler.MVC3」,不過有些地方是需要做些設定上的調整,另外在「MiniProfiler 安全性釋疑」這一篇文章是以 ASP.NET WebForm 來做說明,卻忘了交代 ASP.NET MVC 應該要怎麼做,所以這篇文章就來做個補充說明。


這一次就使用 VS2012 與 ASP.NET MVC 4 來做示範說明。

 

Step.1

準備好一個專案

image

 

Step.2

使用 NuGet 加入 MiniProfiler 相關的套件,要加入的套件有:MiniProfiler, MiniProfiler.MVC3, MiniProfiler.EF.

雖然我們的專案是採用 ASP.NET MVC 4,但還是安裝 MiniProfiler.MVC3,在版本尚未更新的情況下,這一個版本仍然是可以在 ASP.NET MVC 4 的專案中正常使用的,而目前的 MiniPrifiler 版本為 「2.0.2」,

SNAGHTML818a666

加入上面的套件之後,在 App_Start 目錄下會新增一個「MiniProfiler.cs」的檔案,

image

 

Step.3

因為我們的 Model 是使用 ADO.NET Entity Framework,為了要可以監測到每次透過 EF 查詢的 SQL Statement,所以要修改 MiniProfiler.cs 的內容,這在之前相關的 MiniProfiler 文章裡也有交代過,

把 Line 9 ~ 11 移除原本被註解的三個 Namespace using,

image

然後再移除 Line 42 的註解,如果不移除這個註解的話,MiniPrifiler 是不會監測 EF 所執行的 SQL Statement,也就不會顯示在 MiniProfiler 的 Popup 視窗中,

image

 

Step.4

接著就是修改 _Layout.cshtml 的內容,在 _Layout.cshtml 的上面加入StackExchange.Profiling 命名空間的使用,

image

然後在 _Layout.cshtml 裡加入 jQuery 引用的下方加入 MiniProfiler RenderIncludes 方法,

image

 

Step.5

完成上面的步驟之後執行專案,卻沒有看到 MiniProfiler 的 Profiler Popup,

SNAGHTML894c464

開啟 Firebug 看看,卻看到這樣的錯誤訊息,找不到指定的資源檔案,

image

解決這個問題的方式就是要在 Web.Config 裡 system.webServer 區段的 handlers 新增以下的內容,

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

image

加上之後重新建置專案再執行,可以看到 MiniProfiler 相關資源檔案可以正常載入,也可以顯示 Profiler Popup,

SNAGHTML89ac7dd

如果要對 Profiler Popup 做調整的話,可以參考之前的文章內容「MiniProfiler 2.0.1

 

Step.6

在「MiniProfiler 安全性釋疑」裡面有跟大家說明如何在 Web.Config 裡增加一個 appSetting,用來自行調整 Profiler Popup 的顯示與否,那篇文章是以 ASP.NET WebForms 做說明,卻忘了沒有交代 ASP.NET MVC 專案中應該可以怎麼做。

同樣地也在 Web.Config 增加「enable_profiling」的 appSetting 內容,

image

專案一開始有加入 MiniProfiler.MVC3 套件,然後會在 App_Start 目錄下加入一個 MiniProfiler.cs 檔案,這個檔案的下方有以下的 MiniProfilerStartupModule 類別內容,

image

其實這個 MiniProfilerStartupModule 內容就是把原本我們會放在 Global.asax 內的處理給抽出來,抽出來的好處在於可以不用直接寫在網站原本的 Global.asax 內,假使日後不要使用 MiniProfiler,只要把 MiniProfiler 相關檔案與設定移除即可,不用另外去修改 Global.asax 內容,讓 Global.asax 可以單純一些。

原本我們要在 Global.asax 裡面的 Application_BeginRequest(), Application_EndRequest() 增加內容,然後再加上判斷 enable_Profiling 設定以決定是否顯示 Profiler Popup,其實這些方式改在 MiniProfiler.cs 裡面的 MiniProfilerStartupModule 內來做處理,如下所示:

public class MiniProfilerStartupModule : IHttpModule
{
    private bool _enableProfiling = false;
    public bool enableProfiling
    {
        get
        {
            if (!this._enableProfiling)
            {
                this._enableProfiling = bool.Parse(ConfigurationManager.AppSettings["enable_profiling"]);
            }
            return this._enableProfiling;
        }
    }
 
    public void Init(HttpApplication context)
    {
        if (!this.enableProfiling)
        {
            context.BeginRequest += (sender, e) =>
            {
                var request = ((HttpApplication)sender).Request;
                //TODO: By default only local requests are profiled, optionally you can set it up
                //  so authenticated users are always profiled
                if (request.IsLocal) { MiniProfiler.Start(); }
            };
 
 
            // TODO: You can control who sees the profiling information
            /*
            context.AuthenticateRequest += (sender, e) =>
            {
                if (!CurrentUserIsAllowedToSeeProfiler())
                {
                    StackExchange.Profiling.MiniProfiler.Stop(discardResults: true);
                }
            };
            */
 
            context.EndRequest += (sender, e) =>
            {
                MiniProfiler.Stop();
            };
        }
    }
 
    public void Dispose() { }
}

如果你還是習慣在 Global.asax 裡來處理這些事情的話,那就依照之前的做法也是可以的。

 


延伸閱讀:

MiniProfiler 2.0.1

MiniProfiler 安全性釋疑

參考連結:

Scott Allen「ELMAH and MiniProfiler In ASP.NET MVC 4」

 

以上

11 則留言:

  1. 感謝補上mvc4的說明 這套件真是很方便
    現在直接用ip判斷能不能顯示MiniProfiler 省去on off 設定

    還有想請教一件事情
    我用vs2010 + mvc3 + Razor
    一直遇到 括弧對齊的問題
    format document (Ctrl +E + D) 括弧對齊的很奇怪
    如果方便的話 我在msdn發了問題 好像沒有人可解
    http://social.msdn.microsoft.com/Forums/zh-TW/230/thread/7495aa82-e57b-4043-b2dc-754c7c66b650

    您之前用vs2010 有遇過類似的問題嗎?

    ps 每個禮拜四的twMVC聚會 大約都幾點結束呢?

    回覆刪除
    回覆
    1. 每個禮拜四的 twMVC 聚會時間是 PM0730 ~ PM1000

      刪除
    2. 有關於 VS2010 使用 Razor 的不對稱狀況,我並沒有遇到類似的情形出現,
      或許是你的 View 裡面的 Razor Syntax 結構比較複雜所導致,
      如果可以的話,就下週四晚上帶著你的 Notebook 或是專案來參加 twMVC 聚會,大家一起來研究。

      刪除
    3. Razor對齊問題,我在VS2012有碰到,也有人反應給微軟了,但似乎不是每個人都會碰到,會不會修正就不知道了。

      刪除
  2. 感謝您的回答,原來到十點 那過去還來得及太棒了

    回覆刪除
  3. 請問站長,我出現以下錯誤Could not load file or assembly 'MiniProfiler, Version=2.1.0.0,未載入WebActivator.PDB,該如何處理呢?

    我的環境:vs2012,net4.5,EF5

    回覆刪除
    回覆
    1. 就照著錯誤訊息的說明,看看有什麼沒有安裝或缺少的

      刪除
    2. 感謝站長!已經解決了

      刪除
  4. 今天想要試用miniprofiler來研究看看,但我目前裝的版本已經是miniprofile mvc4,可是在App_Start並沒有看到miniprofiler.cs這個檔案,可以再針對此議題做點介紹嗎??

    回覆刪除
    回覆
    1. 最近 MiniProfiler 在做改版的動作,所以有些安裝方式與之前的版本有所不同,
      建議隨時關注官網的訊息
      http://miniprofiler.com/

      刪除

提醒

千萬不要使用 Google Talk (Hangouts) 或 Facebook 及時通訊與我聯繫、提問,因為會掉訊息甚至我是過了好幾天之後才發現到你曾經傳給我訊息過,請多多使用「詢問與建議」(在左邊,就在左邊),另外比較深入的問題討論,或是有牽涉到你實作程式碼的內容,不適合在留言板裡留言討論,請務必使用「詢問與建議」功能(可以夾帶檔案),謝謝。