简体   繁体   English

在ExceptionFilterAttribute-HttpActionExecutedContext中获取控制器的返回类型

[英]Get the controller's return type in ExceptionFilterAttribute - HttpActionExecutedContext

If somewhere within the request lifecycle, an exception is thrown, the HttpActionExecutedContext has a null response. 如果在请求生命周期内某处引发了异常,则HttpActionExecutedContext的响应为null

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
    }
}

What I'm trying to do is return the same expected object, but with an error created in one of the object's fields. 我想做的是返回相同的预期对象,但在对象的字段之一中创建了错误。

Is there a way to figure out what the expected response type is? 有没有办法弄清楚预期的响应类型是什么?

The information you seek is within the provided context. 您寻求的信息在提供的上下文中。 You just have to go a little deeper to find it in the action descriptor. 您只需要更深入一点就可以在动作描述符中找到它。

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM