如何检查Server.GetLastError()= System.Web.UI.ViewStateException?

时间:2016-05-12 11:32:15

标签: asp.net vb.net exception-handling viewstate

我试图按照以下方式做点什么:

If Server.GetLastError() = System.Web.UI.ViewStateException Then
    '   Do something here
End If

但这不起作用并且给我这个:' ViewStateException'是一个类型类,不能在表达式中使用。

所以我尝试过各种变体:

If (Server.GetLastError.GetType = TypeOf (System.Web.UI.ViewStateException)) Then

但那也不起作用。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

您需要更新语法。使用TypeOf Operator (Visual Basic)

Dim lastError As Exception = Server.GetLastError().InnerException
If TypeOf lastError Is System.Web.UI.ViewStateException Then
    '   Do something here
End If

或者您可以尝试转换为您想要的类型,并检查它是否一无所获。

Dim lastError = TryCast(Server.GetLastError().InnerException, System.Web.UI.ViewStateException)
If lastError IsNot Nothing Then
    '   Do something here
End If