How to fix ViewStateException Invalid ViewState?

Usually you don't see Invalid ViewState exception unless you implement some kind of ASP.NET error logger in Global.asax.

Error usually states something like this:

Exception: System.Web.HttpException: The state information is invalid for this page and might be corrupted. ---> System.Web.UI.ViewStateException: Invalid viewstate.

Those errors are usually generated by some automatic submission form engines. Comments and contact forms are mostly effected by this exception. End user never sees those errors, so invalid viewstate errors don't have any value for your error logger. Let's bypass them:

if (ex is HttpException && ex.InnerException is ViewStateException)
{
    _app.Response.Redirect(_app.Request.Url.AbsoluteUri)                
    return
}

See my error logger article for further details.

Posted on Wednesday, June 23, 2010 by | Comments (4) | Add Comment

Comments

Gravatar

Re:How to fix ViewStateException Invalid ViewState?

thanks for the help.

Posted on 7/3/2010 10:43:15 AM by Surge #
Gravatar

Re:How to fix ViewStateException Invalid ViewState?

Hi,

What is the _app. ?

thanks.

Posted on 8/18/2010 6:47:07 AM by selcuk yazar #
Gravatar

Sorry,
I took this if statement from my logger code. _app is HttpContext.Current.ApplicationInstance.

Gravatar

Re:How to fix ViewStateException Invalid ViewState?

Server Error in '/MDCR' Application.
--------------------------------------------------------------------------------

The state information is invalid for this page and might be corrupted.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The state information is invalid for this page and might be corrupted.

Source Error:


[No relevant source lines]


Source File: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\mdcr\5c7639a0\5981344d\App_Web_oja12imh.0.cs Line: 0

Stack Trace:


[FormatException: Invalid character in a Base-64 string.]
System.Convert.FromBase64String(String s) +0
System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +77
System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +37
System.Web.UI.HiddenFieldPageStatePersister.Load() +113

[ViewStateException: Invalid viewstate.
Client IP: 127.0.0.1
Port: 1486
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.3; .NET4.0C; .NET4.0E)
ViewState: /wEPDwUKMTY5ODYyNzkxNQ9kFgICAw9kFgICAQ9kFgJmD2QWAmYPZBYEZg9kFgICAQ88KwAJAQAPFgYeDVNlbGVjdGVkSW5kZXgCDR4IRGF0YUtleXMWAB4LXyFJdGVtQ291bnQCDmQWHGYPZBYCAgEPDxYIHghUYWJJbmRleAEAAB4EVGV4dAUMTm90aWZpY2F0aW9uHgtDb21tYW5kTmFtZQUETW92ZR4PQ29tbWFuZEFyZ3VtZW50BQEwZGQCAQ9kFgICAQ8PFggfAwEAAB8EBQkxLkNvbXBhbnkfBQUETW92ZR8GBQExZGQCAg9kFgICAQ8PFggfAwEAAB8EBRMyLlR5cGUgb2YgT3duZXJzaGlwHwUFBE1vdmUfBgUBMmRkAgMPZBYCAgEPDxYIHwMBAAAfBAUUMy5FbXBsb3ltZW50IFNvdXJjZXMfBQUETW92ZR8GBQEzZGQCBA9kFgICAQ8PFggfAwEAAB8EBRU0LlN0YXRpc3RpY3M6IEN1cnJlbnQfBQUETW92ZR8GBQE0ZGQCBQ9kFgICAQ8PFggfAwEAAB8EBRQ1LkVtcGxveWVlczogQ3VycmVudB8FBQRNb3ZlHwYFATVkZAIGD2QWAgIBDw8WCB8DAQAAHwQFFzYuU3RhdGlzdGljczogTmV3IEhpcmVzHwUFBE1vdmUfBgUBNmRkAgcPZBYCAgEPDxYIHwMBAAAfBAUZNy5TdGF0aXN...]

[HttpException (0x80004005): The state information is invalid for this page and might be corrupted.]
System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) +106
System.Web.UI.ViewStateException.ThrowViewStateError(Exception inner, String persistedState) +14
System.Web.UI.HiddenFieldPageStatePersister.Load() +217
System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +105
System.Web.UI.Page.LoadAllState() +43
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6785
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +242
System.Web.UI.Page.ProcessRequest() +80
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.certreviewform_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\mdcr\5c7639a0\5981344d\App_Web_oja12imh.0.cs:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +7


I have this problem for the past two days and I am breaking my head. Please let me know how to by pass this error .
I am getting above error I don't how to solve it please help me.

Posted on 5/18/2011 5:28:02 PM by john oliveson #
Gravatar

Delete everything in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ and rebuild your solution.

Gravatar

Re:How to fix ViewStateException Invalid ViewState?

^have same problem, proposed solution didn't work for me:P any other ideas?:)

Posted on 11/2/2011 5:30:37 AM by Rufix #

New Comment

Your Name:
Email (for internal use only):
Comment:
 
Code above:

Categories

Recent Tweets

Valid HTML5