如何在VS调试器中浏览我的页面ViewState的内容?

时间:2011-09-13 15:00:20

标签: asp.net viewstate

我有一个大量使用的ASP.net网页。问题是ViewState变得越来越大!该页面有一个ASP.net GridView,具有分页和排序功能。但是,ViewState的大小似乎与页面上的内容完全不成比例。

我想知道如何在Visual Studio 2010调试器中浏览ViewState的内容,以便我可以知道ViewState中保存了哪些数据。

1 个答案:

答案 0 :(得分:1)

我不确定它是否符合您的需求,但您可以查看此工具:

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