Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
C# 如何找到所有控制器和操作_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# 如何找到所有控制器和操作

C# 如何找到所有控制器和操作,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,如何在dotnet core中找到具有其属性的所有控制器和操作? 在.NET Framework中,我使用了以下代码: public static List<string> GetControllerNames() { List<string> controllerNames = new List<string>(); GetSubClasses<Controller>().ForEach(type => controller

如何在dotnet core中找到具有其属性的所有控制器和操作? 在.NET Framework中,我使用了以下代码:

public static List<string> GetControllerNames()
{
    List<string> controllerNames = new List<string>();
    GetSubClasses<Controller>().ForEach(type => controllerNames.Add(type.Name.Replace("Controller", "")));
    return controllerNames;
}
public static List<string> ActionNames(string controllerName)
{
    var types =
        from a in AppDomain.CurrentDomain.GetAssemblies()
        from t in a.GetTypes()
        where typeof(IController).IsAssignableFrom(t) &&
            string.Equals(controllerName + "Controller", t.Name, StringComparison.OrdinalIgnoreCase)
    select t;

    var controllerType = types.FirstOrDefault();

    if (controllerType == null)
    {
        return Enumerable.Empty<string>().ToList();
    }
    return new ReflectedControllerDescriptor(controllerType)
       .GetCanonicalActions().Select(x => x.ActionName).ToList();
}
公共静态列表GetControllerNames()
{
列表控制器名称=新列表();
getSubclass().ForEach(type=>controllerNames.Add(type.Name.Replace(“Controller”),”);
返回控制器名称;
}
公共静态列表操作名(字符串控制器名)
{
变量类型=
从AppDomain.CurrentDomain.GetAssemblys()中的
从a.GetTypes()中的t开始
其中typeof(IController).IsAssignableFrom(t)&&
string.Equals(controllerName+“Controller”、t.Name、StringComparison.OrdinalIgnoreCase)
选择t;
var controllerType=types.FirstOrDefault();
如果(controllerType==null)
{
返回可枚举的.Empty().ToList();
}
返回新的ReflectedControllerDescriptor(controllerType)
.GetCanonicalActions().Select(x=>x.ActionName).ToList();
}

但是它在dotnetcore中不起作用。

如何将
IActionDescriptorCollectionProvider
注入到需要了解这些信息的组件中?它位于
Microsoft.AspNetCore.Mvc.Infrastructure
命名空间中

此组件为您提供应用程序中可用的每个操作。以下是它提供的数据示例:

作为奖励,您还可以评估所有过滤器、参数等



作为补充说明,我想您可以使用反射来查找从
ControllerBase
继承的类型。但是你知道你可以拥有不从它继承的控制器吗?你能写出超越这些规则的约定吗?出于这个原因,注入上述组件使其变得更加容易。您不必担心它会坏。

谢谢您的帮助。这并帮助我解决:)对于使用NSwag的用户,请利用您已经提供的管道:
Program.CreateHostBuilder(新字符串[0]).Build().Services.GetRequiredService()