如何从控制器获取所有动作名

如何从控制器获取所有动作名

问题描述:

我怎么能写code来获得在asp.net MVC从控制器的所有动作的名字呢?

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 anyone know how to do this?

非常感谢。

有这个问题没有通用的解决方案,因为我可以写在 ActionNameSelectorAttribute 派生的自定义属性,并重写 IsValidName 与任何自定义code,甚至code这个名字比较随机GUID。有没有办法让你知道,在这种情况下,行动名称的属性会接受的。

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.

如果您约束您的解决方案只考虑方法名称或内置的 ActionNameAttribute 则可以反映过来类得到的返回公共方法所有的名字一个的ActionResult ,并检查他们是否有一个 ActionNameAttribute 名称属性将覆盖方法名。

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.