Seemingly random invalid viewstate exceptions

by Gareth12. August 2009 14:34

There a number of reasons why you are receiving random exceptions on ASP .NET applications.

You will be receiving these exceptions from the following resources:

  • webresource.axd
  • scriptresource.axd

I have found these exceptions to be caused by badly constructed html pages
and also possibly javascript embedded incorrectly on these pages. There are
also problems which are browser specific as discussed below.

It can also be caused by users posting back the page before its fully loaded
eg form submit. A detailed bug listing for this can be found here

Alot of these errors seem to be caused by the newer browsers(IF8,FF3.5,chrome).

Possible fixes are:

I found this fix somewhere just cant remember where it may have been here

<%  Response.ContentType = "text/HTML"%>
<%  Response.Charset = "utf-8"%>

IE only Fix - place the following in the html head block which will only allow postback
after page fully loaded

 <meta http-equiv="Page-Enter" content="RevealTrans(Duration=0,Transition=0)" />

The next method I found was to override the default page rendering behaviour as below,
so that the hidden fields required by .net are rendered at the top of the page. This will ensure
that even if the page does render completely postback caused by controls and such will still be able
 to validate correctly. I found this example here and translated it to vb .NET

 Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
        ' move the Event Validation and Viewstate Encryption fields from the bottom to the top, 
        ' right in front of the VIEWSTATE hidden field 
        ' This fixes viewstate validation errors with submitting postbacks before a page is fully loaded.
        Dim sw As New System.IO.StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        MyBase.Render(hw)
        Dim pageHTML As String = sw.ToString()
        Dim viewStateStartPoint As Integer
        Dim eventValidationStartPoint As Integer = pageHTML.IndexOf("<input type=""hidden""" & _
        "name=""__EVENTVALIDATION""")
        If eventValidationStartPoint >= 0 Then
            Dim eventValidationEndPoint As Integer = pageHTML.IndexOf("/>", eventValidationStartPoint) + 2
            Dim eventValidationViewstateInput As String = pageHTML.Substring(eventValidationStartPoint, _
            eventValidationEndPoint - eventValidationStartPoint)
            pageHTML = pageHTML.Remove(eventValidationStartPoint, eventValidationEndPoint - eventValidationStartPoint)
            viewStateStartPoint = pageHTML.IndexOf("<input type=""hidden"" name=""__VIEWSTATE""")
            If viewStateStartPoint >= 0 Then
                pageHTML = pageHTML.Insert(viewStateStartPoint, eventValidationViewstateInput)
            End If
        End If
        Dim viewStateEncryptionStartPoint As Integer = pageHTML.IndexOf("<input type=""hidden""" & _ 
        "name=""__VIEWSTATEENCRYPTED""")
        If viewStateEncryptionStartPoint >= 0 Then
            Dim viewStateEncryptionEndPoint As Integer = pageHTML.IndexOf("/>", viewStateEncryptionStartPoint) + 2
            Dim viewStateEncryptionInput As String = pageHTML.Substring(viewStateEncryptionStartPoint, _
            viewStateEncryptionEndPoint - viewStateEncryptionStartPoint)
            pageHTML = pageHTML.Remove(viewStateEncryptionStartPoint, viewStateEncryptionEndPoint - viewStateEncryptionStartPoint)
            viewStateStartPoint = pageHTML.IndexOf("<input type=""hidden"" name=""__VIEWSTATE""")
            If viewStateStartPoint >= 0 Then
                pageHTML = pageHTML.Insert(viewStateStartPoint, viewStateEncryptionInput)
            End If
        End If
        ' write the fixed HTML
        writer.Write(pageHTML)
        sw.Dispose()
        hw.Dispose()
    End Sub

In the unlikely event that you are using a server farm/cluster you may need to ensure the validation key
in the web.config is the same across the servers see the following kb article

 

Tags: , , , , ,

.NET | ASP .NET

Gareth Emslie

soft-cor.com
.NET Developer
Assembly Newbie
Agnitum Beta-Tester
Linux Enthusiast

PayPal - The safer, easier way to pay online.

Google Ads

BlogRoll

Download OPML file OPML