Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取控制器';ExceptionFilterAttribute-HttpActionExecutedContext中的s返回类型_C#_.net_Asp.net Web Api - Fatal编程技术网

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!正是我要找的!