2017-03-08 1 views
1

요청주기의 어딘가에 예외가 발생하면 HttpActionExecutedContextnull 응답이 있습니다. 내가 할 노력하고있어ExceptionFilterAttribute - HttpActionExecutedContext에서 컨트롤러의 반환 형식을 가져옵니다.

public class MyExceptionFilterAttribute : ExceptionFilterAttribute 
{  
    public override void OnException(HttpActionExecutedContext httpActionExecutedContext) 
    { 
     // Get expected return type here 
     // Can't be httpActionExecutedContext.Response.GetType() because response is null 
    } 
} 

같은 예상 개체를 반환하지만, 객체의 필드 중 하나에서 생성 된 오류입니다.

예상되는 응답 유형이 무엇인지 알아낼 방법이 있습니까?

+0

왜 그런가요? 항상 가능하지는 않습니다. – DavidG

+0

예, 클라이언트는 정확한 XML 구조가 리턴 될 것으로 예상합니다. 즉, 클라이언트가 AccountResponse를 예상하고 오류가 발생하면 AccountResponse.Error가 없어도됩니다. – kotsumu

답변

2

정보는 제공된 문맥 내에 있습니다. 액션 디스크립터에서 찾으려면 조금 더 깊이 들어가야합니다.

public class MyExceptionFilterAttribute : ExceptionFilterAttribute 
    public override void OnException(HttpActionExecutedContext httpActionExecutedContext) { 
     // Get expected return type here 
     var expectedReturnType = httpActionExecutedContext.ActionContext.ActionDescriptor.ReturnType; 

    } 
} 
+0

굉장! 정확히 내가 무엇을 찾고 있었는지! – kotsumu

관련 문제