Define result type in controller OnException

I am working on a MVC.NET 2.0 project, where I am trying to include special error handling logic in a special OnException control procedure. Basically I want to be able to determine the type of the result of the controller method in which the unhandled exception was handled so that I can return error data in a specific type-dependent format (json for JsonResult and html for ActionResult). Can someone tell me a way to define this type? I would really appreciate any help.

Thanks in advance

+7
asp.net-mvc controller
source share
1 answer

Assuming you have not changed the default routing:

protected override void OnException(ExceptionContext filterContext) { var action = filterContext.RouteData.Values["action"].ToString(); var type = filterContext.Controller.GetType(); var method = type.GetMethod(action); var returnType = method.ReturnType; //...do whatever here... } 

Good luck

+4
source share

All Articles