C# 仅捕获特定的HttpException

C# 仅捕获特定的HttpException,c#,asp.net,webforms,C#,Asp.net,Webforms,我是aspx webforms的新手 我想捕获Web应用程序中的特定异常-viewstate MAC验证失败 我试过(在Global.asax.cs中): 问题是它捕获了所有未处理的HttpException 实现这一目标的最佳方式是什么 编辑: 进一步检查这个问题时,我发现内部异常是一个ViewStateException,但它似乎没有特定的“errorCode”属性 谢谢这应该可以 if ((lastErrWrapper != null) && (lastErrWrappe

我是aspx webforms的新手

我想捕获Web应用程序中的特定异常-
viewstate MAC验证失败

我试过(在Global.asax.cs中):

问题是它捕获了所有未处理的HttpException

实现这一目标的最佳方式是什么


编辑:

进一步检查这个问题时,我发现内部异常是一个
ViewStateException
,但它似乎没有特定的“errorCode”属性

谢谢

这应该可以

if ((lastErrWrapper != null) && (lastErrWrapper.InnerException != null) 
  && (lastErrWrapper.InnerException is ViewStateException)
{
}

HttpException的设计目的是使所有与HTTP/web相关的内容都可以由一个处理程序捕获,因此您需要深入研究并查看原始异常。ViewStateException可能会捕获几个其他与视图状态相关的错误,但这可能没有问题。

以下是我们在globa.asax中实现的帮助解决ViewState错误的方法:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

    Dim context As HttpContext = HttpContext.Current
    Dim exception As Exception = Server.GetLastError

    'custom exception handling:
    If Not IsNothing(exception) Then

        If Not IsNothing(exception.InnerException) Then

            'ViewState Exception:
            If exception.InnerException.GetType = GetType(ViewStateException) Then
                'The state information is invalid for this page and might be corrupted.

                'Caused by VIEWSTATE|VIEWSTATEENCRYPTED|EVENTVALIDATION hidden fields being malformed
                ' + could be page is submitted before being fully loaded
                ' + hidden fields have been malformed by proxies or user tampering
                ' + hidden fields have been trunkated by mobile devices
                ' + remotly loaded content into the page using ajax causes the hidden fields to be overridden with incorrect values (when a user navigates back to a cached page)

                'Remedy: reload the request page to replenish the viewstate:
                Server.ClearError()
                Response.Clear()
                Response.Redirect(context.Request.Url.ToString, False)
                Exit Sub
            End If

        End If

    End If

End Sub

对于其他未处理的异常,您想做什么?我相信该转换本身可能会导致异常。如果(Server.GetLastError()是HttpException){…}@MNGwinn,你应该像
那样进行测试-这并不重要,我想做一件“不同的事情”,然后当viewstate MAC的
验证失败时
@AndreCalil-你显然是正确的,但这段代码只是一个例子。问题是,所有未终止的HttpException都将获得
ErrorCode==0x80004005
,我如何区分它们呢?@Bassal AFAIK,viewstate验证将抛出一个
HttpException
,因此仅使用错误代码无法区分它。我会测试它是否为
HttpException
,以及消息是否包含
validation
viewstate
MAC
。谢谢,这也是我发现的(请参阅我的编辑)。是否无法查看抛出了哪个视图状态错误?最好的方法可能是在调试时检查viewstateexception的所有属性,以查看是否有任何属性能够区分它。否则,您可以只检查异常消息中的特定“viewstate mac验证失败”。HttpException上的ErrorCode属性不是从exception继承的,ViewStateException也不是从HttpException派生的,因此我不希望看到它。文档说它包含一个HRESULT,但这通常是在Exception.HRESULT中。如果希望得到有用的错误代码,可以检查ViewStateException.HResult并查看其中的内容。评论不会换行。简短的版本,您不会深入了解ErrorCode和HResult属性。这主要是用于遗留COM互操作性。谢谢@MNGwinn,我想我将使用此解决方案,并考虑到这可能会捕获一些其他视图状态异常。。。
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

    Dim context As HttpContext = HttpContext.Current
    Dim exception As Exception = Server.GetLastError

    'custom exception handling:
    If Not IsNothing(exception) Then

        If Not IsNothing(exception.InnerException) Then

            'ViewState Exception:
            If exception.InnerException.GetType = GetType(ViewStateException) Then
                'The state information is invalid for this page and might be corrupted.

                'Caused by VIEWSTATE|VIEWSTATEENCRYPTED|EVENTVALIDATION hidden fields being malformed
                ' + could be page is submitted before being fully loaded
                ' + hidden fields have been malformed by proxies or user tampering
                ' + hidden fields have been trunkated by mobile devices
                ' + remotly loaded content into the page using ajax causes the hidden fields to be overridden with incorrect values (when a user navigates back to a cached page)

                'Remedy: reload the request page to replenish the viewstate:
                Server.ClearError()
                Response.Clear()
                Response.Redirect(context.Request.Url.ToString, False)
                Exit Sub
            End If

        End If

    End If

End Sub