共用方式為


ViewStateException.UserAgent 屬性

定義

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

public:
 property System::String ^ UserAgent { System::String ^ get(); };
public string UserAgent { get; }
member this.UserAgent : string
Public ReadOnly Property UserAgent As String

屬性值

包含使用者代理 (通常是瀏覽器類型) 的 String

範例

下列程式碼範例示範如何實作方法,以還原序列化 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

備註

這個屬性會傳回與 HTTP_USER_AGENT 屬性中 ServerVariables 變數相同的值。

適用於