This documentation is archived and is not being maintained.

ViewStateException Class

Represents the exception that is thrown when the view state cannot be loaded or validated. This class cannot be inherited.


Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public sealed class ViewStateException : Exception, 

The ViewStateException type exposes the following members.

Public methodViewStateExceptionInitializes a new instance of the ViewStateException class.

Public propertyDataGets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception.)
Public propertyHelpLinkGets or sets a link to the help file associated with this exception. (Inherited from Exception.)
Protected propertyHResultGets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception.)
Public propertyInnerExceptionGets the Exception instance that caused the current exception. (Inherited from Exception.)
Public propertyIsConnectedGets a value indicating whether the client is currently connected to the server.
Public propertyMessageGets debugging information about the HTTP request that resulted in a view-state exception. (Overrides Exception.Message.)
Public propertyPathGets the path of the HTTP request that resulted in a view-state exception.
Public propertyPersistedStateGets the contents of the view-state string that, when read, caused the view-state exception.
Public propertyRefererGets the URL of the page that linked to the page where the view-state exception occurred.
Public propertyRemoteAddressGets the IP address of the HTTP request that resulted in a view-state exception.
Public propertyRemotePortGets the port number of the HTTP request that resulted in a view-state exception.
Public propertySourceGets or sets the name of the application or the object that causes the error. (Inherited from Exception.)
Public propertyStackTraceGets a string representation of the immediate frames on the call stack. (Inherited from Exception.)
Public propertyTargetSiteGets the method that throws the current exception. (Inherited from Exception.)
Public propertyUserAgentGets the browser type of the HTTP request that resulted in a view-state exception.

Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetBaseExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Inherited from Exception.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetObjectDataInitializes a new instance of the ViewStateException class with serialized data. (Overrides Exception.GetObjectData(SerializationInfo, StreamingContext).)
Public methodGetTypeGets the runtime type of the current instance. (Inherited from Exception.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringCreates and returns a string representation of the current exception. (Inherited from Exception.)

Protected eventSerializeObjectStateOccurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (Inherited from Exception.)

If an exception occurs when the page loads the view-state information, the page creates a ViewStateException object and passes it as an inner exception within an HttpException exception. The HttpException exception is re-thrown to be handled by the application.

The ViewStateException class stores information about the exception in the Message property. If the request has permission to access the ServerVariables property, then the Message property contains the values returned from the RemoteAddress, RemotePort, UserAgent, PersistedState, Referer, and Path properties; otherwise, the Message property contains a generic view-state error message.

A view-state exception can be the result of a Message Authentication Codes (MAC) validation error. If the page attribute enableViewStateMac is set to true, view-state information is encoded with a MAC identifier. When the view-state information is posted back to the server, the page verifies the encoding to ensure that it has not been altered by the user. If the page cannot validate the MAC encoding on the view-state information, it will throw an HttpException exception with the ViewStateException object as an inner exception.

For MAC validation to succeed, the view-state information must be hashed by the same key when it is sent and received. Within a Web farm, the machine key on each server should be set to a common key.

The following code example demonstrates how to implement a method that deserializes a base64-encoded string and returns an ICollection collection of property settings. The Deserialize method can throw an HttpException exception that contains a ViewStateException object as an inner exception. The example shows how you can catch an HttpException exception and retrieve the properties from the ViewStateException object.

private ICollection LoadControlProperties(string serializedProperties)

    ICollection controlProperties = null;

    // Create an ObjectStateFormatter to deserialize the properties.
    ObjectStateFormatter formatter = new ObjectStateFormatter();

        // Call the Deserialize method.
        controlProperties = (ArrayList)formatter.Deserialize(serializedProperties);
    catch (HttpException e)
        ViewStateException vse = (ViewStateException)e.InnerException;
        String logMessage;

        logMessage = "ViewStateException. Path: " + vse.Path + Environment.NewLine;
        logMessage += "PersistedState: " + vse.PersistedState + Environment.NewLine;
        logMessage += "Referer: " + vse.Referer + Environment.NewLine;
        logMessage += "UserAgent: " + vse.UserAgent + Environment.NewLine;


        if (vse.IsConnected)
            throw e;
    return controlProperties;

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.