Is it possible to list the names of all controllers and their actions programmatically?
I want to implement database driven security for each controller and action. As a developer, I know all controllers and actions and can add them to a database table, but is there any way to add them automatically?
Or, to whittle away at @dcastro 's idea and just get the controllers:
You can use reflection to find all Controllers in the current assembly, and then find their public methods that are not decorated with the
NonAction
attribute.The following will extract controllers, actions, attributes and return types:
If you run this code in linqpad for instance and call
you get the following output:
Use Reflection, enumerate all types inside the assembly and filter classes inherited from
System.Web.MVC.Controller
, than list public methods of this types as actionsI was looking for a way to get Area, Controller and Action and for this I manage to change a little the methods you post here, so if anyone is looking for a way to get the AREA here is my ugly method (which I save to an xml):
Edit: