How to get all action names from a controller

后端 未结 4 1146
逝去的感伤
逝去的感伤 2021-02-04 22:20

How could I write code to get all the action names from a controller in asp.net MVC?

I want to automatically list all the action names from a controller.

Does an

4条回答
  •  抹茶落季
    2021-02-04 22:41

    There's no generic solution for this, because I could write a custom attribute derived from ActionNameSelectorAttribute and override IsValidName with any custom code, even code that compares the name to a random GUID. There is no way for you to know, in this case, which action name the attribute will accept.

    If you constrain your solution to only considering the method name or the built-in ActionNameAttribute then you can reflect over the class to get all the names of public methods that return an ActionResult and check whether they have an ActionNameAttribute whose Name property overrides the method name.

提交回复
热议问题