1

I have an asp.net web api project. In my controllers I have set up the

ExceptionFilterAttribute

To catch any errors at a global level. There are two get requests being fired off from the controller method. They are failing and so I am seeing the exception being raised in the exception filter. However the exception is not showing me details of the failed request. Is it possible to get them? For example 4 GET requests might have been invoked and one of them is failing and the exception is being thrown. But all im seeing is a message saying...

The remote name could not be resolved: 'xx.xx.com'

But I need more details, like the query string etc...

The response object on the web exception is null too :-(

4

1 回答 1

0

在您的OnException方法中,您ExceptionFilterAttribute有一个 type 的参数参数HttpActionExecutedContext。在此类的实例中,您可以访问RequestResponse属性以获取您需要的所有信息,无论是请求还是响应。在该ActionContext属性中,您甚至可以获取所有路由、控制器和操作信息。

var requestHttpMethod = actionExecutedContext.Request.Method;
var requestUri = actionExecutedContext.Request.RequestUri;

var controllerDescriptor = actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor;
var actionDescriptor = actionExecutedContext.ActionContext.ActionDescriptor;

您还可以修改响应对象以返回更合适的错误消息。看看这些类可以为您提供哪些信息。

于 2013-02-20T11:00:23.797 回答