2012-08-30 2 views
5

aspx webforms를 처음 사용합니다.특정 HttpException 만 catch하십시오.

내 웹 응용 프로그램 - Validation of viewstate MAC failed에서 특정 예외를 catch하고 싶습니다.

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

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

문제는 모든 처리되지 않은 HttpExceptions을 잡는다이다 :
나는 (Global.asax.cs에서)이 시도했다.

이것을 달성하는 가장 좋은 방법은 무엇입니까?


편집 :

확인이 문제가 더 나는 내부 예외가 ViewStateException이지만, 특정 "의 errorCode"속성을

감사가 보이지 않는 것으로 확인 동안

+0

당신이 다른 처리되지 않은 예외가 어떤 작업을 원하십니까? – MNGwinn

+2

그 주조는 예외 자체를 유발할 수 있다고 생각합니다. –

+0

@MNGwinn - 정말 중요하지는 않지만 'viewstate MAC의 유효성 검사'일 때 '다른 일'을하고 싶습니다. (예 : Server.GetLastError()가 HttpException 인 경우) failed ' – Bassal

답변

5

해야합니다

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

HttpExce ption은 모든 HTTP/웹 관련 항목을 하나의 핸들러에서 포착 할 수 있도록 설계되었으므로 원본 예외를 조사해야합니다. ViewStateException은 다른 두 가지 뷰 상태 관련 오류를 잡을 수도 있지만 괜찮습니다. 여기

+0

고마워요, 이것 역시 제가 알아 낸 것입니다 (제 편집 참조). 어떤 뷰 상태 오류가 발생했는지 확인할 방법이 없습니까? – Bassal

+0

가장 좋은 방법은 디버그 중에 viewstateexception의 모든 속성을 검사하여 구별 할 수 있는지 확인하는 것입니다. 그렇지 않으면 특정 "viewstate mac의 유효성 검사 실패"에 대한 예외 메시지를 확인할 수 있습니다. – ryanulit

+0

HttpException의 ErrorCode 속성은 Exception에서 상속되지 않으며 ViewStateException은 HttpException에서 파생되지 않으므로이를 볼 수 없습니다. 문서 도구에는 HRESULT가 포함되어 있지만 일반적으로 Exception.HResult에 있습니다. 유용한 오류 코드가 필요하면 ViewStateException.HResult를 확인하고 그 내용을 확인하십시오. – MNGwinn

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 
관련 문제