ViewState Chunking in ASP.NET 2.0 (maxPageStateFieldLength)

I am currently teaching an ASP.NET 2.0 course in Quebec city. I like it because each time I give a training I learn something (and so do my trainees I hope...). This time I learnt something new about the ViewState.

When ViewState in your page become very large it can be a problem as some firewalls and proxies will prevent access to pages containing such huge ViewState sizes. For this purpose ASP.NET 2.0 introduces the ViewState Chunking mechanism. But the first thing I would say is if your ViewState is that big ask yourself why, and the way to optimize (maybe disabling?) it.

So ASP.NET 2.0 enables to split the ViewState's single hidden field into several using the MaxPageStateFieldLength property in the web.config <pages> section. This indicates the maximum bytes size allowed for one viewstate hidden field. If the real size exceeds the value then viewstate is splitted in multiple fields. By default, the attribute is set to -1 which means that no maximum size is defined.

Sample ViewState before: 
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUKLTk2Njk3OTQxNg9kFgICAw9kFgICCQ88KwANAGQYAQUJR3Jp
ZFZpZXcxD2dk4sjERFfnDXV/hMFGAL10HQUnZbk="
/>

Then set in web.config:
<pages maxPageStateFieldLength="40">

Sample ViewState After :
<input type="hidden" name="__VIEWSTATEFIELDCOUNT" id="__VIEWSTATEFIELDCOUNT"
value="3" />
<input type="hidden" name="__VIEWSTATE"
id="__VIEWSTATE" value="/wEPDwUKLTk2Njk3OTQxNg9kFgICAw9kFgICCQ88" />
<input type="hidden" name="__VIEWSTATE1"
id="__VIEWSTATE1" value="KwANAGQYAQUJR3JpZFZpZXcxD2dk4sjERFfnDXV/" />
<input type="hidden" name="__VIEWSTATE2"
id="__VIEWSTATE2" value="hMFGAL10HQUnZbk=" />

Please note THIS IS NOT a way to optimize ViewState size!! This is a way to fix a possible technical problem with huge ViewState size and firewall.

5 Comments

  • Thanks for the information. I was looking for something like this only

  • Hi Laurent,

    This one is new to me too. We're getting some creepy "Base64" errors out of ViewState on some larger pages we have here on our website. Currently there is plans to trim this ViewState size, of course. Do you think this issue including "Invalid charcaters in Base64 string" and "Invalid lenght for a Base64 string" can somehow be at least postponed by using this tweak? Also, can it be done on a page basis?

    Thanks for sharing the info!
    Alexandre

  • Damn! We had to code this feature ourselves for a legacy ASP.NET 1.1 app with an enormous viewstate! I didn't know there was a feature in 2.0 that did it for you...

  • How does splitting up a huge VIEWSTATE hidden field into several chunks make this more palatable for a firewall or proxy server?

    The problem is the excessive amount of data stored in the view state, not that it is all stored in one enormous hidden field.

  • Cool. Thanks for sharing.

Comments have been disabled for this content.