How to get all action names from a controller

后端 未结 4 1188
逝去的感伤
逝去的感伤 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:39

    You can start with:

    Type t = typeof(YourControllerType);
    MethodInfo[] mi = t.GetMethods();
    foreach (MethodInfo m in mi)
    {
        if (m.IsPublic)
            if (typeof(ActionResult).IsAssignableFrom(m.ReturnParameter.ParameterType))
                methods = m.Name + Environment.NewLine + methods;
    }
    

    You'll have to work more to suit your needs.

提交回复
热议问题