2011-09-13 27 views
0

我有一個大量使用的ASP.net網頁。問題是ViewState變得巨大!該頁面有一個帶分頁和排序的ASP.net GridView。但是,ViewState的大小與頁面上的內容完全不成比例。如何在VS調試器中瀏覽我的頁面ViewState的內容?

我想知道如何在Visual Studio 2010調試器中瀏覽ViewState的內容,以便我可以知道在ViewState中保存了什麼數據。

+0

難道真的沒有辦法訪問單個ViewState的成員一樣,我可以與應用程序或會話變量? –

回答

0

我不知道這是否會滿足您的需求,但你可以看看這個工具:

http://www.pluralsight-training.net/community/media/p/51688.aspx

而這裏的輔助類,你看看哪個轉儲ViewState中的內容到一個日誌文件。顯然,你可以根據需要修改它。

// Written by Greg Reddick. http://www.xoc.net 
public static void SeeViewState(string strViewState, string strFilename) 
{ 
    if (strViewState != null) 
    { 
     Debug.Listeners.Clear(); 
     System.IO.File.Delete(strFilename); 
     Debug.Listeners.Add(new TextWriterTraceListener(strFilename)); 

     string strViewStateDecoded = (new System.Text.UTF8Encoding()).GetString(Convert.FromBase64String(strViewState)); 

     string[] astrDecoded = strViewStateDecoded.Replace("<", "<\n").Replace(">", "\n>").Replace(";", ";\n").Split('\n'); 
     Debug.IndentSize = 4; 

     foreach (string str in astrDecoded) 
     { 
      if (str.Length > 0) 
      { 
       if (str.EndsWith("\\<")) 
       { 
        Debug.Write(str); 
       } 
       else if (str.EndsWith("\\")) 
       { 
        Debug.Write(str); 
       } 
       else if (str.EndsWith("<")) 
       { 
        Debug.WriteLine(str); 
        Debug.Indent(); 
       } 
       else if (str.StartsWith(">;") || str.StartsWith(">")) 
       { 
        Debug.Unindent(); 
        Debug.WriteLine(str); 
       } 
       else if (str.EndsWith("\\;")) 
       { 
        Debug.Write(str); 
       } 
       else 
       { 
        Debug.WriteLine(str); 
       } 
      } 
     } 

     Debug.Close(); 
     Debug.Listeners.Clear(); 

     //Get into the debugger after executing this line to see how .NET looks at 
     //the ViewState info. Compare it to the text file produced above. 
     Triplet trp = (Triplet) ((new LosFormatter()).Deserialize(strViewState)); 
    } 
} 

你可以這樣調用:

DebugViewState.SeeViewState(Request.Form("__VIEWSTATE"), "c:\temp\viewstate.txt") 

請參閱此鏈接瞭解詳情:

http://www.xoc.net/works/tips/viewstate.asp

+0

感謝您的回答。不幸的是,當我用viewstate運行它時,你的代碼會拋出異常(它似乎包含一些原始的HTML)。我會看到我能看到的。 –

相關問題