Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在运行asp core 3.1时获取所有操作、控制器和区域名称_Asp.net Mvc_Asp.net Mvc 4_Asp.net Core_Reflection_.net Core - Fatal编程技术网

Asp.net mvc 如何在运行asp core 3.1时获取所有操作、控制器和区域名称

Asp.net mvc 如何在运行asp core 3.1时获取所有操作、控制器和区域名称,asp.net-mvc,asp.net-mvc-4,asp.net-core,reflection,.net-core,Asp.net Mvc,Asp.net Mvc 4,Asp.net Core,Reflection,.net Core,我有一个asp.net core 3.1应用程序,我想在应用程序运行时获取所有控制器、操作和区域名称,就像在mvc中获取带有反射的操作名称一样。 有什么办法吗?试试这个: ControllerFeature controllerFeature = new ControllerFeature(); this.ApplicationPartManager.PopulateFeature(controllerFeature); IEnumerable<TypeInfo> typeInfos

我有一个asp.net core 3.1应用程序,我想在应用程序运行时获取所有控制器、操作和区域名称,就像在mvc中获取带有反射的操作名称一样。 有什么办法吗?

试试这个:

ControllerFeature controllerFeature = new ControllerFeature();
this.ApplicationPartManager.PopulateFeature(controllerFeature);
IEnumerable<TypeInfo> typeInfos = controllerFeature.Controllers;
ApplicationPartManager必须在类中使用DI。

尝试以下操作:

ControllerFeature controllerFeature = new ControllerFeature();
this.ApplicationPartManager.PopulateFeature(controllerFeature);
IEnumerable<TypeInfo> typeInfos = controllerFeature.Controllers;
1.型号:

public class ControllerActions
{
    public string Controller { get; set; }
    public string Action { get; set; }
    public string Area { get; set; }
}
2.显示控制器、动作和区域名称:

[HttpGet]
public List<ControllerActions> Index()
{
    Assembly asm = Assembly.GetExecutingAssembly();
    var controlleractionlist = asm.GetTypes()
            .Where(type => typeof(Controller).IsAssignableFrom(type))
            .SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
            .Select(x => new
            {
                Controller = x.DeclaringType.Name,
                Action = x.Name,
                Area = x.DeclaringType.CustomAttributes.Where(c => c.AttributeType == typeof(AreaAttribute))

            }).ToList();
    var list = new List<ControllerActions>();
    foreach (var item in controlleractionlist)
    {
        if (item.Area.Count() != 0)
        {
            list.Add(new ControllerActions()
            {
                Controller = item.Controller,
                Action = item.Action,
                Area = item.Area.Select(v => v.ConstructorArguments[0].Value.ToString()).FirstOrDefault()
            });
        }
        else
        {
            list.Add(new ControllerActions()
            {
                Controller = item.Controller,
                Action = item.Action,
                Area = null,
            });
        }
    }
    return list;
}

总有办法的。但是你必须先试试你自己:看看:你想得到所有控制器、动作和区域名称的场景是什么?我应该在哪里使用这些代码?你能举个例子吗!!