{"id":624,"date":"2004-08-17T07:55:01","date_gmt":"2004-08-17T11:55:01","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=624"},"modified":"2004-08-17T07:55:01","modified_gmt":"2004-08-17T11:55:01","slug":"aspnet-the-view-state-is-invalid-for-this-page-and-might-be-corrupted","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/08\/17\/aspnet-the-view-state-is-invalid-for-this-page-and-might-be-corrupted\/","title":{"rendered":"ASP.NET:  The View State is invalid for this page and might be corrupted"},"content":{"rendered":"<p>I fixed a tricky bug yesterday on one of our sites that runs ASP.NET.  Like all good webmasters, anytime a 500 error occurs on the site, an email is sent to me that contains all the juicy details: the URL the user was visiting, the date\/time, what browser they were using, which server in the cluster it occured on, the stack trace, any form variables, the querystring, session variables, and cookies.  This particular error would occur on any page with a form and the stack trace would look like this:<br \/>\n<code><br \/>\nException of type System.Web.HttpUnhandledException was thrown.<br \/>\nsource = System.Web<br \/>\ntarget site = Boolean HandleError(System.Exception)<br \/>\nstack trace = at System.Web.UI.Page.HandleError(Exception e) at<br \/>\nSystem.Web.UI.Page.ProcessRequestMain() at<br \/>\nSystem.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequest(HttpContext context) at System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutio<br \/>\nnStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep<br \/>\nstep, Boolean&amp; completedSynchronously)<br \/>\n<\/code><br \/>\nwhich isn&#8217;t very helpful at all. So my first problem was that I wasn&#8217;t getting enough information; I couldn&#8217;t tell what the problem really was.  The HttpModule that I wrote that listens for <code>Application_Error<\/code> events was retrieving the exception using this code:<br \/>\n<code><br \/>\nHttpApplication application = (HttpApplication)source;<br \/>\nException error = application.Server.GetLastError();<br \/>\n<\/code><br \/>\nwhich technically does retrieve the error that caused the event, but I was missing one key step: the last line should have read:<br \/>\n<code><br \/>\nException error = application.Server.GetLastError().InnerException;<br \/>\n<\/code><br \/>\nand once I fixed that, I magically started receiving a much richer error description:<br \/>\n<code><br \/>\nSystem.Web.HttpException: The viewstate is invalid for this page and might be corrupted. at System.Web.UI.Page.LoadPageStateFromPersistenceMedium() at System.Web.UI.Page.LoadPageViewState() at System.Web.UI.Page.ProcessRequestMain()<br \/>\n<\/code><br \/>\nThat description leads to a couple different knowledge base articles on <a href=\"http:\/\/support.microsoft.com\/\">support.microsoft.com<\/a>; <a href=\"http:\/\/support.microsoft.com\/default.aspx?scid=kb;EN-US;323744\">Q323744<\/a> and <a href=\"http:\/\/support.microsoft.com\/default.aspx?scid=kb;EN-US;312906\">Q312906<\/a> were the ones that fixed the problem I was experiencing.  Turns out that when you a) run an ASP.NET application in a cluster (in my case it&#8217;s an ASP.NET application load balanced behind with an <a href=\"http:\/\/www.f5.com\/\">F5<\/a>), b) don&#8217;t use sticky sessions, and c) utilize view state, you must setup your web servers to use the exact same machine key, which is a setting in machine.config.  Microsoft supplies a C# script that will generate the appropriate machineKey element for you machine.config in Q312906.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I fixed a tricky bug yesterday on one of our sites that runs ASP.NET. Like all good webmasters, anytime a 500 error occurs on the site, an email is sent to me that contains all the juicy details: the URL the user was visiting, the date\/time, what browser they were using, which server in the &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/08\/17\/aspnet-the-view-state-is-invalid-for-this-page-and-might-be-corrupted\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ASP.NET:  The View State is invalid for this page and might be corrupted<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,12],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/624"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=624"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/624\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}