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.)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With