Troubleshooting the "view State is invalid" error with ASP. NET

Source: Internet
Author: User
Tags html encode
Document directory
  • Verify that you are not running into issues that have been fixed
  • Set the validationkey attribute if you are running in a web farm
  • Do not store dynamically generated types in view State in a web farm
  • Determine whether the problem is related to the view State Mac feature
  • Determine exactly what exception occurs when you receive the error message
  • Try storing the view State in the session
  • Determine whether the problem is caused by Worker Process reconfiguring

Original article: http://support.microsoft.com/kb/829743/en-us

 

Troubleshooting the "view State is invalid" error with ASP. netview products that this Article applies.

On this pageexpand all | collapse all

SummaryThis article describes some techniques that can be used to debug and to resolve problems with view State in Microsoft ASP. NET applications.Back to the top

Introductionview state is a feature in ASP. net that allows pages to automatically preserve State without relying on server state (for example, session state ). however, issues relating to view State can be difficult to debug. in most cases, when problems with view State occur, you receive the following error message in the web browser, with little indication of what might be causing the issue: "The viewstate is invalid for this page and might be specified upted"

This article describes some techniques that can be used for debugging and for resolving problems with view State. Back to the top

More informationverify that you are not running into issues that have been fixed

A number of view State issues were fixed with ASP. NET 1.0 hotfixes and service packs, and those fixes are also part of ASP. NET 1.1. make sure that you have applied the latest fixes before tracking issues that have already been resolved. you can obtain the latest Microsoft. net Framework updates from the following Microsoft Developer Network (msdn) Web site: http://msdn2.microsoft.com/en-us/netframework/aa569276.aspxBack to the topset the validationkey attribute if you are running in a web farm

In a web farm, each client request can go to a different machine on every PostBack. Because of this behavior, you cannot leaveValidationkeyAttribute SetAutogenerateIn the machine. config file. Instead, you must set the value ofValidationkeyAttribute to a fixed string that is shared by all the machines on the Web farm.

For more information about this issue, click the following article number to view the article in the Microsoft Knowledge Base: 323744 fix: "The View state is invalid for this page and might be specified upted" error message in ASP. netback to the topdo not store dynamically generated types in view State in a web farm

When ASP. net compiles files dynamically, the files are built into assemblies with essential random names (for example, a file name might be jp395dun. DLL ). if you are running a web farm, the same files will be compiled into assemblies with different random names. normally, this is not a problem because no one makes assumptions on those assembly names. but if you ever put a dynamically compiled type into view State by using binary serialization, the name of the Assembly will be encoded as part of the view State data. when that view State is later sent to a different server in the Web farm, the view State cannot be deserialized because it uses different assembly names.

The best fix to this problem is to avoid using binary serialization. binary serialization uses runtime resources even when you do not run into this problem. instead, limit what you put in view State to a combinationArrays,Pairs,Triplets, And simple types (for example,Strings,Int, And Other types ).System. Web. UI. PairAndSystem. Web. UI. TripletAre simple wrapper types that the view state engine can efficiently process.

An alternative fix to avoid this problem is to move the types that you are storing in view State into a precompiled assembly, either in your bin folder or in the global assembly cache. this fix does not address performance, but it guarantees that the Assembly has the same name on all computers.

NoteIf you store complex data types in view State and experience this issue, the call stack information will contain in stacks that are similar to the following:

[FileNotFoundException: Could not load file or assembly 'App_Web_fx--sar9, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.]  System.RuntimeTypeHandle._GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean loadTypeFromPartialName) +0 System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark) +72 System.RuntimeType.PrivateGetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark) +58 System.Type.GetType(String typeName, Boolean throwOnError) +57 System.Web.UI.ObjectStateFormatter.DeserializeType(SerializerBinaryReader reader) +192  System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader) +943  System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader) +384  System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader) +198  System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader) +210  System.Web.UI.ObjectStateFormatter.DeserializeValue(SerializerBinaryReader reader) +198  System.Web.UI.ObjectStateFormatter.Deserialize(Stream inputStream) +142
Back to the topdetermine whether the problem is related to the view State Mac feature

The purpose of the view state machine authentication code (MAC) feature is to make it impossible for clients to send a request that contains a malicious view State. by default, this feature is enabled in the following flag in the machine. config file.

enableViewStateMac="true"

The simplest way to determine whether the issue you are dealing with is related to the Mac feature is to turn off the feature. to do this, change the flag in the machine. config file to the following code.

enableViewStateMac="false"

If you no longer get view State errors, the problem is related to the Mac feature.

ImportantOnly turn off the view State Mac feature to help diagnose the problem. you shoshould not keep the view State Mac turned off to work around the issue. if so, you cocould introduce security holes. for more information, visit the following msdn Web site: http://msdn2.microsoft.com/en-us/library/aa302388.aspx

If you turn the view State Mac feature off, and then you use view State for controls that do not HTML encode (for example,LabelControl), attackers can tamper with the view State data and can put arbitrary data in view State. this arbitrary data is decoded and then used by controls when they render the posted page. as a result, attackers can inject script into the application unless you work to prevent the attack. for example, an attacker cocould decode the data, inject script into the data whereLabelControl is, and then link to it from a web site. anyone who clicks on the Link wocould be the victim of a script injection attack that cocould potentially steal their authentication cookies or session ID. the script cocould also let an attacker alter State data for controls that use view State and Application Specific Attacks cocould occur as a result.

In general, microsoft recommends that you not turn off the view State Mac feature unless you are completely confident that you have either disabled view State for all controls that do not HTML encode their output (for example,DataGridControls,DatalistControls,LabelControls, and other controls) or that you are always explicitly setting their values on each request to something known to be safe.

For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base: 316920 you receive a "view State is invalid" error message when you use the server. transfer method324488 Forms authentication and view State fail intermittently under heavy loadback to the topdetermine exactly what exception occurs when you receive the error message

Unfortunately, the invalid view state error message that is mentioned in the "Introduction" section of this article is not very informative. the error message is generally caused by some exception being thrown when the view State is being processed. the problem is that the exception is being consumed, and its details are lost in the error message.

By using a debugger, you can determine the original exception. to do this, you must attach a debugger to the ASP. net Process (aspnet_wp.exe or w3wp.exe), and then set it to catch all exceptions. the debugger will probably stop at a few exceptions that are not relevant, but eventually it will hit the view State exception and provide useful information for troubleshooting.

The following steps are an example that uses the runtime debugger (cordbg.exe ).

  1. At a command prompt, run the iisreset command to make sure you are provided with an good starting point, and then browse to a page on your site.
  2. At a command prompt, run cordbg.exe.
  3. At the command prompt, type pro, and then press Enter. A list of managed processes will appear. You shoshould see either the aspnet_wp.exe process or the w3wp.exe process. Note its PID.
  4. At the prompt, typePIDTo attach to the process.

    NoteReplacePIDWith the PID that was noted in step 3.

  5. At the prompt, typeCA ETo tell cordbg.exe to break on all privileges tions, and then type G to let the process run.
  6. Whenever you get an exception, type w to see the stack. if the stack is a view State exception (look for loadpagestatefrompersistencemedium on the stack), copy all the exception and the stack information from the command window, and then save the information. this information can help you understand the problem. if the exception is unrelated, type G.

For more information, click the following article number to view the article in the Microsoft Knowledge Base: 831150 the "viewstate is invalid for this page" error message does not provide sufficient information to troubleshoot the issueback to the toptry storing the view State in the session

By default, the view State is round-tripped by means of<Input type = hidden>Field that is sent to the browser. the browser then sends the field back to the server on the next request. in some cases, this view State can get quite large and be a potential source of problems. some browsers cannot handle such a large hidden field (and the resulting large request), and the browsers may truncate the view State. truncating the view State causes a "view State failed upted" error message. this behavior is most likely to occur in simpler browsers. for example, this behavior may occur in a browser on a PDA.

To determine whether you may be running into such an issue, try storing the view State in the session. The following example demonstrates how to do this.

<%@ language=c# debug=true %>   <script runat=server>  protected override object LoadPageStateFromPersistenceMedium()  {       return Session["_ViewState"];  }   protected override void SavePageStateToPersistenceMedium(object viewState)  {       Session["_ViewState"] = viewState;  }   void TextChanged(object o, EventArgs e)  {       Response.Write("TextChanged");  }  </script>  <form runat=server>  <asp:button text=Test runat=server/>  <asp:textbox ontextchanged=TextChanged runat=server/>  <input type=hidden name=__VIEWSTATE>  </form>  

The following line of code is only needed in ASP. NET 1.0, to work around a bug. in ASP. NET 1.1, it is not necessary.

<input type=hidden name=__VIEWSTATE>

Back to the topdetermine whether the problem is caused by Worker Process reconfiguring

Consider the following scenario.

  • You are running ASP. NET under Microsoft Internet Information Services (IIS) 6.0.
  • The application pool is running under an identity other than the local system account, the network service account, or an administrative-level account.
  • TheValidationkeyAttribute of the <machinekey> element is set to autogenerate in the configuration file.

In this scenario, the following procedure will cause a view state error to occur:

  1. A user browses a page.
  2. The worker process that hosts the ASP. NET application recycles.
  3. The user posts back the page.
Back to the topthe workaround for this scenario is to use an explicit ValidationkeyAttribute in the configuration file. for more information about how to create a key, click the following article numbers to view the articles in the Microsoft Knowledge Base: 312906 how to create keys by using Visual C #. net for use in forms authentication313091 how to create keys by using Visual Basic. net for use in forms authenticationback to the top

Referencesfor more information, click the following article numbers to view the articles in the Microsoft Knowledge Base: 316920 "view State is invalid" error message when you use server. transfer324488 Forms authentication and view State fail intermittently under heavy load831150 the "viewstate is invalid for this page" error message does not provide sufficient information to troubleshoot the issue

For more information, visit the following msdn web sites: http://msdn2.microsoft.com/en-us/library/aa302388.aspx

Http://msdn2.microsoft.com/en-us/netframework/aa731542.aspxBack to the top


Applies Toback to the top
Keywords: Kbstate kbwebforms kbinfo kbprb kb829743
Back to the top

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.