How to get ActionDescriptor using action and controller names

Viewed 11192

Given the action name, controller name and HTTP verb (GET, POST .. etc), is it possible to check whether the action has (ie. is decorated by) a specific action filter attribute?

Please note: The action and controller are not the current action and controller but can be any action and controller in the app.

Thanks!

4 Answers

In case someone writes unit tests and controllerFactory is not working for him as in Miguel Angelo's answer, this is the best I came up with so far:

var ctrl = // your controller instance of type T.
var controllerDescriptor = new ReflectedControllerDescriptor(ctrl.GetType());
var actionDescriptor = new ReflectedActionDescriptor(typeof(T).GetMethod(actionName), actionName, controllerDescriptor);

It doesn't locate the controller itself (it assumes the controller), but you can usually do this when writing tests...

Related