5

aspx Webフォームは初めてです。

Web アプリケーションで特定の例外をキャッチしたい - Validation of viewstate MAC failed.
私はこれを試しました(Global.asax.csで):

protected void Application_Error(object sender, EventArgs e)
{
  HttpException lastErrWrapper = Server.GetLastError() as HttpException;

  if ((uint)lastErrWrapper.ErrorCode == 0x80004005)
  {
      // do something
  }         
}

問題は、未処理の HttpExceptions をすべてキャッチすることです。

これを達成する最良の方法は何ですか?


編集:

この問題をさらに確認しているときに、内部例外が であることがわかりましたViewStateExceptionが、特定の「errorCode」属性を持っていないようです

ありがとう

4

2 に答える 2

5

これでできるはず

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

HttpException は、すべての HTTP/Web 関連のものを 1 つのハンドラーでキャッチできるように設計されているため、元の例外を掘り下げて調べる必要があります。ViewStateException は、他のいくつかのビュー ステート関連のエラーをキャッチする場合がありますが、おそらく問題ありません。

于 2012-08-30T13:48:42.287 に答える
1

以下は、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
于 2014-02-15T12:00:25.310 に答える