Url remapping could be the source of your ViewState validation woes!

At work, we’re preparing a new release of the website, and came across an issue regarding ViewState validation. You may (or hopefully may not) have come across the following error before:

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
Or the even less helpful:

Unable to validate data.
Now originally, I thought this was something to do width MAC validation of the ViewState. But this is on a local machine, not a web farm. I shouldn’t need to worry about the MachineKey because of that. Hmmm…

Now, this only happens on two of our critical landing pages, which again was odd. They all inherit from the same base type, they all do exactly the same thing, albeit with different textual copy, so why would it happen?

Turns out, its our dynamic url remapping. When we do a Server.Transfer the ViewState validation fails, because the target page is not the same url as the remapped page we came from. The solution is simple, change the Form.Action property of the page to point to the remapped page:

protected new void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);

Form.Action = "/{remapped-url-here}.aspx";
Code language: JavaScript (javascript)

}
Quite obvious now I think about it.

Leave a Reply

Your email address will not be published.

Related Post