ASP.NET: Seeing What is in the ViewState in ASP.NET Web Forms |
Search [dffgdbde] |
||
Previous Next Xoc Software Training RVBA Conventions Maya Calendar Program Company Information Tools ASP.NET and Other Tips .NET: Debugging Designer Features of a Custom Control in Visual Studio Articles.NET: Setting the Default Font in a Windows Mobile/Compact Framework Custom Control .NET Fixing C# XML Comments so they Work in Windows XP SP2 .NET: Getting and Setting the Application Version Number .NET: Getting the Path of the Executing Assembly .NET: Retrieving Assembly Attributes .NET: Setting the RootFolder to Other Values in the FolderBrowserDialog in .NET .NET: Sizing Columns in a ListView Control in .NET .NET: Using Remoting in .NET ASP.NET: Constructing a Graphic on the Fly in ASP.NET ASP.NET: Controlling Caching in ASP.NET Web Forms ASP.NET: How to use the FrontPage Server Extensions with ASP.NET ASP.NET: Seeing What is in the ViewState in ASP.NET Web Forms ASP.NET: Using Forms Authentication in ASP.NET ASP.NET: View Trace Information on your ASP.NET Web Pages ASP: Create XML from an ADO query ASP: Detect Incomplete Loads ASP: Including an ASP.NET Web Page In a Classic ASP Web Page ASP: Process .HTM Files with Scripts and Server Side Includes ASP: QuickSort Algorithm ASP: Retrieve all server variables from IIS ASP: Send Email from Active Server Page HTML: How to Create a Non-Scrolling Region in HTML IE: Allowing Only Certain ActiveX Controls to Run in Internet Explorer IIS: Creating a web site for testing in IIS Server IIS: Creating Multiple Web Sites within IIS on Windows 2000 and Windows XP Professional IIS: IIS/Visual InterDev Problems and Fixes IIS: Redirect a domain such as xoc.net to www.xoc.net SQL Server: Execute SQL Server Updategram Web Design: Design for People with Disabilities Web Design: Keep a Web Page out of the Google Cache Windows: Get HTTP Header of a Web Page using Telnet Windows: Testing Domain Names without DNS Windows: Using Hosts File to Access Web Sites with XP SP2 Windows: Windows XP Command Line Tools Windows Mobile: Reprogramming the Push-to-Talk Button on the AT&T Tilt Miscellaneous Downloads Links Search 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 . All Rights Reserved. 02/20/09 01:28 |