Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI HttpActionExecutedContext get controller name

Tags:

I need to get the controller who triggers a filter attribute.

I have the following filter:

public override void OnException(HttpActionExecutedContext filterContext) {     if (filterContext == null) {         throw new ArgumentNullException("filterContext");     }       if (filterContext.Exception != null) {          // string controllerName = (string) filterContext.....??          // string actionName = (string) filterContext.....?          HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.InternalServerError) {             Content = new StringContent("An unhandled exception was thrown by Customer Web API controller."),                 ReasonPhrase = "An unhandled exception was thrown by Customer Web API controller."         };          filterContext.Response = msg;       }  } 

In traditional MVC this was easy by doing:

string controllerName = (string) filterContext.RouteData.Values["controller"]; string actionName = (string) filterContext.RouteData.Values["action"]; 

Any clue? Appreciate it

like image 649
VAAA Avatar asked Aug 14 '14 13:08

VAAA


1 Answers

Finally I found it:

filterContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName filterContext.ActionContext.ActionDescriptor.ActionName 

Thanks

like image 149
VAAA Avatar answered Sep 20 '22 15:09

VAAA