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:
-1275465166
dd

Text(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