Home > Uncategorized > Bad page viewstate in asp.net

Bad page viewstate in asp.net

Everybody who has worked with ASP.NET will be aware of the page viewstate. .NET’s way of persisting session information using hidden values on the page. However, you may find that if you accidentally post a viewstate from one page to another, or for some reason the worker process looses track of the page viewstate (such as within a server farm, or, as I suspect, where multiple worker processes are handling the same client) – you get an error such as the following:
 
[HttpException (0x80004005): Unable to validate data.]
   System.Web.Configuration.MachineKey.GetDecodedData(Byte[] buf, Byte[] modifier, Int32 start, Int32 length, Int32& dataLength) +195
   System.Web.UI.LosFormatter.Deserialize(String input) +60
[HttpException (0x80004005): Authentication of viewstate failed.  1) If this is a cluster, edit <machineKey> configuration so all servers use the same validationKey and validation algorithm.  AutoGenerate cannot be used in a cluster.  2) Viewstate can only be posted back to the same page.  3) The viewstate for this page might be corrupted.]
   System.Web.UI.LosFormatter.Deserialize(String input) +118
   System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +101
[HttpException (0x80004005): Invalid_Viewstate
 
You can of course, use persistant storage, such as SQL server to store the viewstate, but if it is just a rare occurance, you can have the page handle the event gracefully as follows
 

protected override object LoadPageStateFromPersistenceMedium()

{

object pageState;

try

{

pageState = base.LoadPageStateFromPersistenceMedium();

}

catch

{

// Something has gone wrong, reload page back to a stable state.

pageState = null;

Response.Redirect(Request.RawUrl);

}

return pageState;

}

 
That is, if the viewstate’s bad. then reload the page without any post data.
 
You can also return null from this procedure without redirecting, but this may have some unusual side effects.
Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: