Catch only specific HttpException

I am new to aspx web forms.

I want to catch a specific exception in my web application - Validation of viewstate MAC failed.
I tried this (in Global.asax.cs):

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

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

The problem is that it catches all raw HttpExceptions.

What is the best way to achieve this?


edit:

While checking this problem, I found that the internal exception is this ViewStateException, but it does not have a specific attribute "errorCode"

thank

+5
source share
2 answers

It should do it

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

HttpException , HTTP/web , . ViewStateException , , .

+5

, ViewState globa.asax:

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
+1

All Articles