如何从控制器获取所有动作名 [英] How to get all action names from a controller

查看:101
本文介绍了如何从控制器获取所有动作名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能写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.

这篇关于如何从控制器获取所有动作名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆