ViewState Encoding in ASP.NET 2.0

Onion Blog

Syndication

I've been working on a piece for the October edition of MSDN magazine on ViewState in ASP.NET 2.0 over the last couple of days, and thought I'd share a few of the things I've found through my blog, as sort of a 'sneak preview'.

If you have ever looked at the ViewState string in an ASP.NET page, it looks like a garbled mess (this is the ViewState string from an ASP.NET 1.1 form with a textbox, a dropdownlist, and a label after being posted back to):

dDwtNTgzMzY0OTAwO3Q8O2w8aTwzPjs+O2w8dDw7bDxpPDc+Oz47bDx0
PHA8cDxsPFRleHQ7PjtsPEhlbGxvIFNhbSBob3cgZG8geW91IGxpa2Ug
YmVpbmcgYSBMYXd5ZXI7Pj47Pjs7Pjs+Pjs+Pjs+

Which of course is because the contents of ViewState is base64-encoded for safe transmission in a POST request. If you decode the ViewState string into its raw string format, you can see the serialization format that ASP.NET uses to encode objects stored in ViewState:

t<-583364900;t<;l<i<3>;>;l<t<;l<
i<7>;>;l<t<p<p<l<Text;>;l<
Hello Sam how do you like being a Lawyer;>>;>;;>
;>>;>>;>

In 1.1 they use a tuple format (a hierarchical collection of triplets and pairs) serialized using < and > characters. The letter preceding the < symbol indicates the type of the object stored (t=triplet, p=pair, i=integer, etc.). Each sub-element within the < and > characters is separated with a ';' character. It's an interesting serialization format, sort of like a compressed XML.

If you want to try this yourself, here's a little console app to decode a base-64 encoded string (note that this assumes all data is character format and stuffs it into a string for viewing purposes - in reality the purpose of base64 encoding is to take binary data and 'stringify' it for transmission so much of the data will likely be binary)

using System;
using System.Text;

class App
{
  static void Main(string[] args)
  {
    if (args.Length != 1)
    {
      Console.WriteLine("Usage: decode <input string>");
      return;
    }
	
    byte[] conv = Convert.FromBase64String(args[0]);
    UTF8Encoding encoding = new UTF8Encoding(false);
    char[] buf = encoding.GetChars(conv);
	
    StringBuilder sb = new StringBuilder();
    foreach (char c in buf)
      sb.Append(c);
	
    Console.WriteLine(sb.ToString());
  }
}

I also have a utility you can use to decode ViewState available for 1.1 here and 2.0 here. I have a new version that displays control state as well in 2.0 which I will post soon (with an accompanying blog entry).

Now, if we take the same form and run it under ASP.NET 2.0 (May preview release) it generates the following ViewState string:

/wEPDwULLTEyNzU0NjUxNjYPZBYCAgMPZBYCAgcPDxYCHgRUZX
h0BShIZWxsbyBTYW0gaG93IGRvIHlvdSBsaWtlIGJlaW5nIGEg
TGF3eWVyZGRk

Which decoded looks like:

??????-1275465166?d?????d?????????Text?(Hello Sam how do you like being a Lawyerddd

... at least when rendered in a browser (if you decode it in a console application you will see lots of cool characters like smiley faces and hearts :). It turns out that instead of using a collection of paired <, > symbols with key letters to indicate type, 2.0 uses non-printable characters to delineate elements. To see the characters more clearly, here is a rendition of the same decoded ViewState string using the &#c; notation to represent the non-printable characters:

&#1;&#15;&#15;&#5;&#11;-1275465166
&#15;d&#22;&#2;&#2;&#3;&#15;d&#22;
&#2;&#2;&#7;&#15;&#15;&#22;&#2;&#30;
&#4;Text&#5;(Hello Sam how do you like being a Lawyerddd

This shift to using non-printable characters to delineate the objects stored in ViewState serves two purposes. One, it improves the efficiency of the lexical analysis during the parsing of the ViewState string since there is no longer any need to match characters or parse tokens. Second, and more important, is the fact that it reduces the number of characters used to encode the objects in ViewState. In the sample ViewState string I showed you above, the number of characters used as 'delineation' characters in the 1.1 string is 58 versus 22 for the 2.0 string. These characters actually add up pretty quickly and can contribute significantly to the overall size of ViewState on a page. For example, if you place a DataGrid on a form and bind it to the authors table in the pubs database, the size of the ViewState string in 1.1 is 13,132 characters. If you do the same in 2.0, the size of ViewState is reduced to 6,024 characters - a reduction of more than half.

So at the very least, you can look forward to an immediate reduction in the size of your ViewState with the release of ASP.NET 2.0. I'll cover some more of the new features of ViewState 2.0 soon...


Posted Jun 03 2004, 09:50 AM by fritz-onion
Filed under:

Comments

Onion Blog wrote Control state in ASP.NET 2.0
on 07-01-2004 10:02 AM
VGA wrote re: ViewState Encoding in ASP.NET 2.0
on 07-12-2004 11:31 AM
Past May I've posted some low-level details[1] on the new viewstate serializer that you may be interested in.

[1] http://weblogs.asp.net/vga/archive/2004/05/26/WhidbeyWillBringsUsAShorterViewstateGuaranteed.aspx
Daniel Cazzulino wrote re: ViewState Encoding in ASP.NET 2.0
on 07-12-2004 11:43 AM
A much more comprehensive analysis of what's changed in Whidbey ViewState: http://weblogs.asp.net/vga/archive/2004/05/26/WhidbeyWillBringsUsAShorterViewstateGuaranteed.aspx
Fritz Onion wrote re: ViewState Encoding in ASP.NET 2.0
on 07-14-2004 11:20 AM
Thanks for the pointer VGA - I hadn't run across your blog before, but it's got great information. Subscribed!
-Fritz
Fritz Onion wrote re: ViewState Encoding in ASP.NET 2.0
on 01-04-2005 2:51 PM
test
Coding Horror wrote Wrangling ASP.NET Viewstate
on 10-12-2005 11:39 PM
Inspired by Scott Hanselman's recent post on ASP.NET viewstate wrangling, here's a roundup of tips for dealing with that ornery viewstate stuff. The first rule of thumb, of course, is to turn it off whenever you can. But sometimes...
Mayur Joshi wrote re: ViewState Encoding in ASP.NET 2.0
on 02-06-2006 8:53 PM
Hi Onion Blog

This is very useful infromation. This will give clear insite how ASP.NET 2.0 handle view state.
If you have any more details on ASP.NET 2.0 please forward it to me on joshi.m.n@gmail.com

Regards
Mayur
Ramachandran D wrote re: ViewState Encoding in ASP.NET 2.0
on 03-02-2006 9:04 PM
Cool about view state. But i did not understand why these additional characters are added??? may be a stupid question.
dr3d wrote re: ViewState Encoding in ASP.NET 2.0
on 08-18-2006 9:09 AM
Version 2.0 doesn't work in August 2006

Try entering, for example, http://atlas.asp.net - and you get "IEWSTATE" as the viewstate string.
Same thing any url I try.

Is there an update somewhere?
Rexiology@MSDN wrote ASP.NET ViewState Decoders...
on 05-25-2007 4:02 AM
As I mentioned in my post couple days ago that I am doing some web application performance evaluation
Rexiology::Work wrote ASP.NET ViewState Decoders...
on 05-25-2007 4:04 AM
crosspost from http://blogs.msdn.com/rextang As I mentioned in my post couple days ago that I am doing

Add a Comment

(required)  
(optional)
(required)  
Remember Me?