views:

320

answers:

2

We know that behind the scenes, the ASP.NET MVC framework will use reflection to determine what controllers/actions are available to be executed, based on which classes derive from System.Web.Mvc.Controller and, of those classes, which methods return an ActionResult object.

To my question - is it possible to access this list of controllers/actions from within my MVC application?

(I could do it myself, by using reflection on the current assembly, but if the list has already been built by ASP.NET MVC, I'd rather re-use that effort than re-invent the wheel myself.)

+4  A: 

This is done in an internal class in the System.Web.Mvc assembly called System.Web.Mvc.ControllerTypeCache.

By the way, action methods are not required to return ActionResult. They can return void happily, for instance.

Mehrdad Afshari
Might be worth clarifying then what consitutes an action on a controller. Any public method on a controller can be seen as an action I believe.
AnthonyWJones
Yeah. Unless explicitly declared as `[NonAction]`, of course.
Mehrdad Afshari
+3  A: 

new ReflectedControllerDescriptor(typeof(TController)).GetCanonicalActions() will return a collection of ActionDescriptor objects showing all the actions on the controller. It's not smart enough to understand things like selection attributes or naming attributes, so not every action it returns is guaranteed to be web-callable. But if you need to execute the actions directly, you can call ActionDescriptor.Execute() on any action of interest to you.

Levi