Server Side Viewstate in ASP.NET 2.0
We had viewstate problems when we rolled out schwans.com using ASP.NET 1.0 (some user agents could not handle the large hidden field and other times we had corruptions that caused the MAC check to fail on postback, read about it at http://aspalliance.com/72). We solved the problem by writing our own solution for storing the viewstate in Session. ASP.NET 2.0 now provides built in support for this! It utilizes a page adapter and the new SessionPageStatePersister class.
The first step is to create a page adapter that simply returns an instance of the SessionPageStatePersister class rather than the default HiddenFieldPageStatePersister class which stores viewstate in a hidden field on the client. Create a class library that contains the following and compile into an assembly:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
namespace RJB
{
public class PageStateAdapter : System.Web.UI.Adapters.PageAdapter
{
public override PageStatePersister GetStatePersister()
{
return new SessionPageStatePersister(this.Page);
}
}
}
Then create a .browser that specifies that the new page adapter should be used for all browsers (example is named pageStateAdapter.browser and placed in App_Browsers folder locally with the web site):
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.Page" adapterType="RJB.PageStateAdapter" />
</controlAdapters>
</browser>
</browsers>
Now as requests come in from any type of browser it will use the new adapter which returns a SessionPageStatePersister instance. You can verify that it is working properly by viewing a page on the site then use trace.axd and view the session information and verify that it contains viewstate info (__VIEWSTATEQUEUE and __SESSIONSTATE... entries). Viewing the client page source will also show that the majority of the viewstate is no longer stored on the client (not sure what is left on the client...)