开发者

Get Action and Action Parameters in Controller.OnException

We're using the OnException virtual method in BaseController开发者_JS百科 to log our exception.

But how can we get the controller action and parameters that the exception originated from?


You can get all these data from ExceptionContext object.

For example using this code you can get controller, action and all other routing parameters:

context.RouteData.Values

Using this code you can get query string parameters:

context.HttpContext.Request.QueryString

And finnaly form parameters:

context.HttpContext.Request.Form


protected override void OnException(ExceptionContext filterContext)
{
    string action = filterContext.RouteData.Values["action"].ToString();
    string controller = filterContext.RouteData.Values["controller"].ToString();
}


As far as Action Parameters go, you will need to pass them explicitly.

We don't provide the action arguments on ExceptionContext because it would most often be empty.

Source: https://github.com/aspnet/Mvc/issues/6154

Which is stupid IMO.

UPDATE: You could have your error handler class implement ActionFilterAttribute, which implements:
public virtual void OnActionExecuting(ActionExecutingContext filterContext)

where filterContext.ActionParameters are what you are looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜