ViewStateException 類別

定義

表示當檢視狀態無法載入或驗證時所擲回的例外狀況。 此類別無法獲得繼承。

public ref class ViewStateException sealed : Exception
[System.Serializable]
public sealed class ViewStateException : Exception
[<System.Serializable>]
type ViewStateException = class
    inherit Exception
    interface ISerializable
Public NotInheritable Class ViewStateException
Inherits Exception
繼承
ViewStateException
屬性
實作

範例

下列程式碼範例示範如何實作方法,以還原序列化 base64 編碼的字串,並傳回 ICollection 屬性設定的集合。 方法 Deserialize 可以擲回 HttpException 包含 物件做為內部例外狀況的例外 ViewStateException 狀況。 此範例示範如何攔截 HttpException 例外狀況,並從 物件擷 ViewStateException 取屬性。

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 內部例外狀況傳遞。 應用程式 HttpException 會重新擲回例外狀況以進行處理。

類別 ViewStateException 會將例外狀況的相關資訊儲存在 屬性中 Message 。 如果要求具有存取 ServerVariables 屬性的許可權,則 Message 屬性會包含從 RemoteAddressRemotePort 、、 UserAgentPersistedStateRefererPath 屬性傳回的值;否則, Message 屬性會包含泛型檢視狀態錯誤訊息。

檢視狀態例外狀況可能是訊息驗證碼 (MAC) 驗證錯誤的結果。 如果頁面屬性 enableViewStateMac 設定 true 為 ,則檢視狀態資訊會以 MAC 識別碼編碼。 當檢視狀態資訊傳回伺服器時,頁面會驗證編碼,以確保使用者尚未變更。 如果頁面無法在檢視狀態資訊上驗證 MAC 編碼,它會擲回例外狀況, ViewStateException 並將物件當做內部例外狀況擲回 HttpException 例外狀況。

若要讓 MAC 驗證成功,檢視狀態資訊必須在傳送和接收時由相同的金鑰雜湊。 在 Web 服務器陣列中,每部伺服器上的電腦金鑰都應該設定為通用金鑰。

建構函式

ViewStateException()

初始化 ViewStateException 類別的新執行個體。

屬性

Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。

(繼承來源 Exception)
IsConnected

取得值,指出用戶端目前是否與伺服器連接著。

Message

取得造成檢視狀態例外狀況之 HTTP 要求的偵錯資訊。

Path

取得造成檢視狀態例外狀況之 HTTP 要求的路徑。

PersistedState

取得當讀取時造成檢視狀態例外狀況之檢視狀態字串的內容。

Referer

取得頁面的 URL,這個頁面連結到發生檢視狀態例外狀況的頁面。

RemoteAddress

取得造成檢視狀態例外狀況之 HTTP 要求的 IP 位址。

RemotePort

取得造成檢視狀態例外狀況之 HTTP 要求的通訊埠編號。

Source

取得或設定造成錯誤的應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。

(繼承來源 Exception)
UserAgent

取得造成檢視狀態例外狀況之 HTTP 要求的瀏覽器類型。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

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

使用序列化資料,初始化 ViewStateException 類別的新執行個體。

GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。

(繼承來源 Exception)

事件

SerializeObjectState
已淘汰.

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

適用於

另請參閱