2011-09-13 28 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)。我会看到我能看到的。 –

相关问题