Asp.net mvc 如何从控制器获取所有操作名称

Asp.net mvc 如何从控制器获取所有操作名称,asp.net-mvc,Asp.net Mvc,如何编写代码从asp.net MVC中的控制器获取所有操作名称 我想自动列出控制器中的所有动作名称 有人知道怎么做吗 非常感谢 使用反射是一个很好的开始。没有通用的解决方案,因为我可以编写一个从ActionNameSelectorAttribute派生的自定义属性,并用任何自定义代码覆盖IsValidName,甚至是将名称与随机GUID进行比较的代码。在本例中,您无法知道属性将接受哪个操作名称 如果您将解决方案限制为仅考虑方法名称或内置的ActionNameAttribute,则您可以在类上进行

如何编写代码从asp.net MVC中的控制器获取所有操作名称

我想自动列出控制器中的所有动作名称

有人知道怎么做吗


非常感谢

使用反射是一个很好的开始。

没有通用的解决方案,因为我可以编写一个从
ActionNameSelectorAttribute
派生的自定义属性,并用任何自定义代码覆盖
IsValidName
,甚至是将名称与随机GUID进行比较的代码。在本例中,您无法知道属性将接受哪个操作名称

如果您将解决方案限制为仅考虑方法名称或内置的
ActionNameAttribute
,则您可以在类上进行反思,以获取返回
ActionResult
的公共方法的所有名称,并检查它们是否具有其
name
属性覆盖方法名称。

您可以从以下内容开始:

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;
}

你需要做更多的工作来满足你的需要。

我已经为这个问题绞尽脑汁了一段时间,我相信我已经想出了一个解决方案,大部分时间都应该是可行的。它涉及为相关控制器获取一个
ControllerDescriptor
,然后检查
ControllerDescriptor.GetCanonicalActions()
返回的每个
ActionDescriptor

[ChildActionOnly]
public ActionResult Navigation()
{
    // List of links
    List<string> NavItems = new List<string>();

    // Get a descriptor of this controller
    ReflectedControllerDescriptor controllerDesc = new ReflectedControllerDescriptor(this.GetType());

    // Look at each action in the controller
    foreach (ActionDescriptor action in controllerDesc.GetCanonicalActions())
    {
        bool validAction = true;

        // Get any attributes (filters) on the action
        object[] attributes = action.GetCustomAttributes(false);

        // Look at each attribute
        foreach (object filter in attributes)
        {
            // Can we navigate to the action?
            if (filter is HttpPostAttribute || filter is ChildActionOnlyAttribute)
            {
                validAction = false;
                break;
            }
        }

        // Add the action to the list if it's "valid"
        if (validAction)
            NavItems.Add(action.ActionName);
    }

    return PartialView(NavItems);
}
我最终在我的控制器中执行了一个返回部分视图的操作,但我认为很容易弄清楚发生了什么,所以请随意使用代码并根据您的需要进行更改

[ChildActionOnly]
public ActionResult Navigation()
{
    // List of links
    List<string> NavItems = new List<string>();

    // Get a descriptor of this controller
    ReflectedControllerDescriptor controllerDesc = new ReflectedControllerDescriptor(this.GetType());

    // Look at each action in the controller
    foreach (ActionDescriptor action in controllerDesc.GetCanonicalActions())
    {
        bool validAction = true;

        // Get any attributes (filters) on the action
        object[] attributes = action.GetCustomAttributes(false);

        // Look at each attribute
        foreach (object filter in attributes)
        {
            // Can we navigate to the action?
            if (filter is HttpPostAttribute || filter is ChildActionOnlyAttribute)
            {
                validAction = false;
                break;
            }
        }

        // Add the action to the list if it's "valid"
        if (validAction)
            NavItems.Add(action.ActionName);
    }

    return PartialView(NavItems);
}
[ChildActionOnly]
公共行动结果导航()
{
//链接列表
List NavItems=新列表();
//获取此控制器的描述符
ReflectedControllerDescriptor controllerDesc=新的ReflectedControllerDescriptor(this.GetType());
//查看控制器中的每个操作
foreach(controllerDesc.GetCanonicalActions()中的ActionDescriptor操作)
{
布尔有效性=真;
//获取操作的任何属性(筛选器)
object[]attributes=action.GetCustomAttributes(false);
//查看每个属性
foreach(属性中的对象过滤器)
{
//我们可以导航到动作吗?
if(筛选器为HttpPostAttribute | |筛选器为ChildActionOnlyAttribute)
{
有效性=假;
打破
}
}
//如果操作“有效”,则将其添加到列表中
if(有效期)
NavItems.Add(action.ActionName);
}
返回部分视图(NavItems);
}

可能还有更多的过滤器需要注意,但目前这适合我的需要。

你能给我举个简单的例子吗?谢谢你给我举个例子。这非常有帮助。