C# 获取控制器';ExceptionFilterAttribute-HttpActionExecutedContext中的s返回类型
如果在请求生命周期内的某个地方抛出异常,则C# 获取控制器';ExceptionFilterAttribute-HttpActionExecutedContext中的s返回类型,c#,.net,asp.net-web-api,C#,.net,Asp.net Web Api,如果在请求生命周期内的某个地方抛出异常,则HttpActionExecutedContext具有null响应 public class MyExceptionFilterAttribute : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext httpActionExecutedContext) { // Get expected re
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
}
}
我试图做的是返回相同的预期对象,但在对象的一个字段中创建了一个错误
是否有办法确定预期的响应类型?您查找的信息在提供的上下文中。你只需要再深入一点,在动作描述符中找到它
public class MyExceptionFilterAttribute : ExceptionFilterAttribute
public override void OnException(HttpActionExecutedContext httpActionExecutedContext) {
// Get expected return type here
var expectedReturnType = httpActionExecutedContext.ActionContext.ActionDescriptor.ReturnType;
}
}
你需要这个有什么原因吗?这并不总是可能的,是的,客户端希望返回一个精确的XML结构。这意味着如果客户端需要AccountResponse,并且发生了错误,则需要没有AccountResponse.ErrorSome!正是我要找的!