ViewStateException Class


表示当无法加载或验证视图状态时引发的异常。Represents the exception that is thrown when the view state cannot be loaded or validated. 此类不能被继承。This class cannot be inherited.

public ref class ViewStateException sealed : Exception
public sealed class ViewStateException : Exception
type ViewStateException = class
    inherit Exception
    interface ISerializable
Public NotInheritable Class ViewStateException
Inherits Exception


下面的代码示例演示如何实现一个方法, 该方法可对 base64 编码的字符串进行反ICollection序列化, 并返回属性设置的集合。The following code example demonstrates how to implement a method that deserializes a base64-encoded string and returns an ICollection collection of property settings. Deserialize方法可能会HttpException引发一个异常, 其中ViewStateException包含作为内部异常的对象。The Deserialize method can throw an HttpException exception that contains a ViewStateException object as an inner exception. 该示例演示如何捕获HttpException异常并ViewStateException从对象中检索属性。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;
Private Function LoadControlProperties(ByVal serializedProperties As String) As ICollection

    Dim controlProperties As ICollection = Nothing

    ' Create an ObjectStateFormatter to deserialize the properties.
    Dim formatter As New ObjectStateFormatter()

        ' Call the Deserialize method.
        controlProperties = CType(formatter.Deserialize(serializedProperties), ArrayList)
    Catch e As HttpException
        Dim vse As ViewStateException
        Dim logMessage As String

        vse = e.InnerException

        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) Then
            Throw e
        End If
    End Try
    Return controlProperties
End Function 'LoadControlProperties   


如果在页面加载视图状态信息时发生异常, 则该页将创建一个ViewStateException对象并将其作为内部异常传递到HttpException异常中。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. HttpException异常将被重新引发, 由应用程序进行处理。The HttpException exception is re-thrown to be handled by the application.

ViewStateExceptionMessage在属性中存储有关异常的信息。The ViewStateException class stores information about the exception in the Message property. 如果请求有权访问ServerVariables属性, 则属性将Message包含PersistedStateRemoteAddressReferer RemotePort UserAgent、、、和Path返回的值。属性否则, Message属性包含一般视图状态错误消息。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.

视图状态异常可能是消息身份验证代码 (MAC) 验证错误的结果。A view-state exception can be the result of a Message Authentication Codes (MAC) validation error. 如果将页属性enableViewStateMac设置为true, 则使用 MAC 标识符对视图状态信息进行编码。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. 如果该页无法在视图状态信息上验证 MAC 编码, 则会引发HttpException异常, ViewStateException并将对象作为内部异常。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.

要使 MAC 验证成功, 必须在发送和接收视图状态信息时, 使用相同的密钥对其进行哈希处理。For MAC validation to succeed, the view-state information must be hashed by the same key when it is sent and received. 在 Web 场中, 每台服务器上的计算机密钥都应该设置为一个公用密钥。Within a Web farm, the machine key on each server should be set to a common key.



初始化 ViewStateException 类的新实例。Initializes a new instance of the ViewStateException class.



获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)

获取或设置指向与此异常关联的帮助文件链接。Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

(Inherited from Exception)

获取一个值,该值指示客户端当前是否连接在服务器上。Gets a value indicating whether the client is currently connected to the server.


获取导致视图状态异常的 HTTP 请求的调试信息。Gets debugging information about the HTTP request that resulted in a view-state exception.


获取导致视图状态异常的 HTTP 请求的路径。Gets the path of the HTTP request that resulted in a view-state exception.


获取视图状态字符串的内容,读取它时导致了视图状态异常。Gets the contents of the view-state string that, when read, caused the view-state exception.


获取页的 URL,该页链接到发生视图状态异常的页。Gets the URL of the page that linked to the page where the view-state exception occurred.


获取导致视图状态异常的 HTTP 请求的 IP 地址。Gets the IP address of the HTTP request that resulted in a view-state exception.


获取导致视图状态异常的 HTTP 请求的端口号。Gets the port number of the HTTP request that resulted in a view-state exception.


获取或设置导致错误的应用程序或对象的名称。Gets or sets the name of the application or the object that causes the error.

(Inherited from Exception)

获取调用堆栈上的即时框架字符串表示形式。Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)

获取引发当前异常的方法。Gets the method that throws the current exception.

(Inherited from Exception)

获取导致视图状态异常的 HTTP 请求的浏览器类型。Gets the browser type of the HTTP request that resulted in a view-state exception.



确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext)

用序列化数据初始化 ViewStateException 类的新实例。Initializes a new instance of the ViewStateException class with serialized data.


获取当前实例的运行时类型。Gets the runtime type of the current instance.

(Inherited from Exception)

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)

创建并返回当前异常的字符串表示形式。Creates and returns a string representation of the current exception.

(Inherited from Exception)



当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(Inherited from Exception)

Applies to

See also