ViewStateException 類別

定義

表示當檢視狀態無法載入或驗證時所擲回的例外狀況。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
[System.Serializable]
public sealed class ViewStateException : Exception
type ViewStateException = class
    inherit Exception
    interface ISerializable
Public NotInheritable Class ViewStateException
Inherits Exception
繼承
ViewStateException
屬性
實作

範例

下列程式碼範例示範如何執行還原ICollection序列化 base64 編碼字串的方法, 並傳回屬性設定的集合。The following code example demonstrates how to implement a method that deserializes a base64-encoded string and returns an ICollection collection of property settings. 方法可以擲回包含ViewStateException物件當做內部例外狀況的例外狀況。HttpException DeserializeThe 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();

    try
    {
        // 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;

        LogEvent(logMessage);

        if (vse.IsConnected)
        {
            HttpContext.Current.Response.Redirect("ErrorPage.aspx");
        }
        else
        {
            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()

    Try
        ' 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

        LogEvent(logMessage)

        If (vse.IsConnected) Then
            HttpContext.Current.Response.Redirect("ErrorPage.aspx")
        Else
            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.

類別會將例外狀況的相關資訊儲存Message在屬性中。 ViewStateExceptionThe ViewStateException class stores information about the exception in the Message property. 如果ServerVariables要求具有存取屬性的許可權, Message則屬性會包含PersistedStateRemoteAddressRemotePortUserAgent、、 RefererPath傳回的值。屬性否則, 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()

初始化 ViewStateException 類別的新執行個體。Initializes a new instance of the ViewStateException class.

屬性

Data

取得提供例外狀況之其他使用者定義相關資訊的索引鍵/值組集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(繼承來源 Exception)
HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。Gets or sets a link to the help file associated with this exception.

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。Gets the Exception instance that caused the current exception.

(繼承來源 Exception)
IsConnected

取得值,指出用戶端目前是否與伺服器連接著。Gets a value indicating whether the client is currently connected to the server.

Message

取得造成檢視狀態例外狀況之 HTTP 要求的偵錯資訊。Gets debugging information about the HTTP request that resulted in a view-state exception.

Path

取得造成檢視狀態例外狀況之 HTTP 要求的路徑。Gets the path of the HTTP request that resulted in a view-state exception.

PersistedState

取得當讀取時造成檢視狀態例外狀況之檢視狀態字串的內容。Gets the contents of the view-state string that, when read, caused the view-state exception.

Referer

取得頁面的 URL,這個頁面連結到發生檢視狀態例外狀況的頁面。Gets the URL of the page that linked to the page where the view-state exception occurred.

RemoteAddress

取得造成檢視狀態例外狀況之 HTTP 要求的 IP 位址。Gets the IP address of the HTTP request that resulted in a view-state exception.

RemotePort

取得造成檢視狀態例外狀況之 HTTP 要求的通訊埠編號。Gets the port number of the HTTP request that resulted in a view-state exception.

Source

取得或設定造成錯誤的應用程式或物件的名稱。Gets or sets the name of the application or the object that causes the error.

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。Gets a string representation of the immediate frames on the call stack.

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。Gets the method that throws the current exception.

(繼承來源 Exception)
UserAgent

取得造成檢視狀態例外狀況之 HTTP 要求的瀏覽器類型。Gets the browser type of the HTTP request that resulted in a view-state exception.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

使用序列化資料,初始化 ViewStateException 類別的新執行個體。Initializes a new instance of the ViewStateException class with serialized data.

GetType()

取得目前執行個體的執行階段類型。Gets the runtime type of the current instance.

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。Creates and returns a string representation of the current exception.

(繼承來源 Exception)

事件

SerializeObjectState

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(繼承來源 Exception)

適用於

另請參閱