|
ASP.NET: Seeing What is in the ViewState in ASP.NET Web Forms |
Search [dffgdbde] |
|
|
Other Xoc managed sites: http://grr.xoc.net http://www.986faq.com http://www.mayainfo.org https://mayacalendar.xoc.net http://www.yachtslog.com |
Have you ever wondered what is actually encoded in the __VIEWSTATE hidden variable on your Web Forms? It's actually a base64 serialized encoding of a datatype called a Triplet, that has other objects nested within it. I threw together a small class that has a single static method that will write the viewstate to a file. It indents to see the hierarchy. It also deserializes the viewstate into a Triplet object that you can view in the debugger. This code is only for debugging, and should not be left in the shipping application. [Visual Basic] Call the method in the Page_Load event of your Web Form, like this:
DebugViewState.SeeViewState(Request.Form("__VIEWSTATE"), "c:\temp\viewstate.txt")
and include this class in the project:
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.Strings
Imports System
Imports System.Diagnostics
Imports System.Web.UI
Public Class DebugViewState
'Written by Greg Reddick. http://www.xoc.net
Public Shared Sub SeeViewState(ByVal strViewState As String, ByVal strFilename As String)
If Not strViewState Is Nothing Then
Debug.Listeners.Clear()
System.IO.File.Delete(strFilename)
Debug.Listeners.Add(New TextWriterTraceListener(strFilename))
Dim strViewStateDecoded As String = _
(New System.Text.UTF8Encoding()).GetString(Convert.FromBase64String(strViewState))
Dim astrDecoded As String() = strViewStateDecoded.Replace("<", "<" _
& vbCr).Replace(">", vbCr & ">").Replace(";", ";" _
& vbCr).Split(Convert.ToChar(vbCr))
Dim str As String
Debug.IndentSize = 4
For Each str In astrDecoded
If str.Length > 0 Then
If Right(str, 2) = "\<" Then
Debug.Write(str)
ElseIf Right(str, 1) = "\" Then
Debug.Write(str)
ElseIf Right(str, 1) = "<" Then
Debug.WriteLine(str)
Debug.Indent()
ElseIf str = ">;" Or str = ">" Then
Debug.Unindent()
Debug.WriteLine(str)
ElseIf Right(str, 2) = "\;" Then
Debug.Write(str)
Else
Debug.WriteLine(str)
End If
End If
Next
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.
Dim trp As Triplet = CType((New LosFormatter()).Deserialize(strViewState), Triplet)
End If
End Sub
End Class
[C#] Call the method in the Page_Load event of your Web Form, like this: DebugViewState.SeeViewState(Request.Form["__VIEWSTATE"], @"c:\temp\viewstate.txt"); and include this class in the project:
using System;
using System.Diagnostics;
using System.Web.UI;
public class DebugViewState
{
// 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));
}
}
}
|
||
|
Top |
[www.xoc.net] Copyright © 1997-2023 by Gregory Reddick |
||