开发者

How to browse the contents of my page ViewState in the VS debugger?

I have an ASP.net web page that is heavily used. The problem is that the ViewState is becoming huge! The page has an ASP.net GridView with paging and sorting. However, the size of the ViewState seems totally out of proportion with what's on the page.

I would like to know how to browse the contents of the Vi开发者_JAVA百科ewState in the Visual Studio 2010 debugger so that I can know what data is being saved in the ViewState.


I'm not sure if it will suit your needs, but you can check out this tool:

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

And here's helper class you check out which dumps the contents of the ViewState to a log file. Obviously, you can modify it as needed.

// 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));
    }
}

And you can call it like this:

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

See this link for more details:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜