【问题标题】:How to browse the contents of my page ViewState in the VS debugger?如何在 VS 调试器中浏览我的页面 ViewState 的内容?
【发布时间】:2011-09-13 15:00:19
【问题描述】:

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

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

【问题讨论】:

  • 真的没有办法像使用 Application 或 Session 变量那样访问单个 ViewState 成员吗?

标签: asp.net viewstate


【解决方案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

【讨论】:

  • 感谢您的回答。不幸的是,当我使用视图状态运行您的代码时(它似乎包含一些原始 HTML),您的代码会引发异常。我会看看我能辨别什么。
猜你喜欢
  • 1970-01-01
  • 2012-12-11
  • 2012-04-19
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多