Tuesday, January 24, 2012

Ultimate method to disable the viewstate of asp.net web page

Recently I was working on a ASP.Net project template which can accommodate both asp.net web forms and MVC 3 screens together. I started with a MVC 3 application and added a web page .I could see that MVC3 template is missing some configurations in the web.config which makes it little difficult to work. Finally I could see the web forms running along with the MVC views after some changes. But when I put a button inside the webpage and wired its click event the things started breaking. The error I was getting tells about the view state.


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.


With the stack trace as

[FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. ]
   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() +147

[ViewStateException: Invalid viewstate. 
 Client IP: ::1
 Port: 
 Referer: http://localhost:49849/WebForm1.aspx
 Path: /WebForm1.aspx
 User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
 ViewState: /wEPDwUKMjA0OTM4MTAwNGRk,/wEPDwUKMjA0OTM4MTAwNGRk]

[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) +235
   System.Web.UI.ViewStateException.ThrowViewStateError(Exception inner, String persistedState) +14
   System.Web.UI.HiddenFieldPageStatePersister.Load() +251
   System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +106
   Mvc3TestApp.WebForm1.LoadPageStateFromPersistenceMedium() in E:\Joy\Code\DotNet\ASPDOTNET\MVC\Mvc3TestApp\Mvc3TestApp\WebForm1.aspx.cs:27
   System.Web.UI.Page.LoadAllState() +43
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +8431
   System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +253
   System.Web.UI.Page.ProcessRequest() +78
   System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
   System.Web.UI.Page.ProcessRequest(HttpContext context) +49
   ASP.webform1_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\09b72d60\350d6099\App_Web_uhhehyfz.0.cs:0
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +100
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

Disabling ASP.Net page viewstate


I have compared the code and config with a newly created normal asp.net web forms application.But not able to find any difference. Then I decided to go with disabling the view state.

As everybody does I disabled the view state at the page directive as follows

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm1.aspx.cs" Inherits="Mvc3TestApp.WebForm1" 
Debug="true" EnableViewState="false" 
EnableViewStateMac="false" EnableSessionState="True" 
ViewStateMode="Disabled" ViewStateEncryptionMode="Never" 
EnableEventValidation="false" %>

It didn't fix the issue.Then I started disabling the view state for the controls present in the web page.Still the same error. Then I started google and could see some ideas such as disabling in the web.config.But still the viewstate came and it was not able to deserialize properly.

This makes me to do something myself and started debugging. After some time I could see that I can disable by overriding the LoadPageStateFromPersistenceMedium method. It was very simple.

protected override object LoadPageStateFromPersistenceMedium()
{
            return null;
            //return base.LoadPageStateFromPersistenceMedium();
}

2 comments:

Razo said...

Works perfect to me, thank you very much ...!!

Blogger said...

If you want your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you must watch this video
right away...

(VIDEO) Text Your Ex Back?