2017-03-08 60 views
1

如果请求生命周期中的某个位置出现异常,则的响应为null在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

相关问题