Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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# 如何获取ApicController所有操作的列表_C#_Asp.net_Asp.net Mvc_Api - Fatal编程技术网

C# 如何获取ApicController所有操作的列表

C# 如何获取ApicController所有操作的列表,c#,asp.net,asp.net-mvc,api,C#,Asp.net,Asp.net Mvc,Api,我试图使用此代码,但它不起作用(返回空列表) 研究了如何获得ApiController方法签名而不是控制器 string Items = ""; IEnumerable<Assembly> Assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>(); foreach (Assembly FoundAssembly in Assemblies)

我试图使用此代码,但它不起作用(返回空列表)


研究了如何获得ApiController方法签名而不是控制器

        string Items = "";
        IEnumerable<Assembly> Assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();

        foreach (Assembly FoundAssembly in Assemblies)
        {
            string AssemblyName = FoundAssembly.FullName;
            IEnumerable<TypeInfo> Types = FoundAssembly.DefinedTypes.Where(type => type != null && type.IsPublic && type.IsClass && !type.IsAbstract && typeof(ApiController).IsAssignableFrom(type));
            foreach (TypeInfo ControllerType in Types)
            {
                System.Web.Http.Controllers.ApiControllerActionSelector ApiControllerSelection = new System.Web.Http.Controllers.ApiControllerActionSelector();
                System.Web.Http.Controllers.HttpControllerDescriptor ApiDescriptor = new System.Web.Http.Controllers.HttpControllerDescriptor(new System.Web.Http.HttpConfiguration(), ControllerType.Name, ControllerType);
                ILookup<string, System.Web.Http.Controllers.HttpActionDescriptor> ApiMappings = ApiControllerSelection.GetActionMapping(ApiDescriptor);

                foreach (var Maps in ApiMappings)
                {
                    foreach (System.Web.Http.Controllers.HttpActionDescriptor Actions in Maps)
                    {
                        Items += "[ controller=" + ControllerType.Name + " action=" + ((System.Web.Http.Controllers.ReflectedHttpActionDescriptor)(Actions)).MethodInfo + "]";
                    }
                }
            }
        }

还有一个额外的控制器“ODataMetaController”,我没有创建它。可能是由初始项目创建添加的。我也没有考虑过这是否可以简化。

我从程序集中获取所有apiController,然后在foreach
BuildManager.GetReferencedAssemblys()中尝试从当前类型(apiController)获取操作。其中(type=>type!=null&&type.IsPublic&&type.IsClass&&type.IsAsStract&&typeof(ApiController)。IsAssignableFrom(type)
foreach(GetControllers()中的var控制器)var controllerDescriptor=new ReflectedControllerDescriptor(控制器)
我仍在寻找这个问题的解决方案-就我而言,我怀疑它不起作用,因为ReflectedControllerDescriptor来自.Net MVC库,而ApicController来自较新的(独立的)Web API库。我相信你是对的。将typeof(ApiController)切换为typeof(Controller)要获取MVC,控制器将填写操作名称。
        string Items = "";
        IEnumerable<Assembly> Assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();

        foreach (Assembly FoundAssembly in Assemblies)
        {
            string AssemblyName = FoundAssembly.FullName;
            IEnumerable<TypeInfo> Types = FoundAssembly.DefinedTypes.Where(type => type != null && type.IsPublic && type.IsClass && !type.IsAbstract && typeof(ApiController).IsAssignableFrom(type));
            foreach (TypeInfo ControllerType in Types)
            {
                System.Web.Http.Controllers.ApiControllerActionSelector ApiControllerSelection = new System.Web.Http.Controllers.ApiControllerActionSelector();
                System.Web.Http.Controllers.HttpControllerDescriptor ApiDescriptor = new System.Web.Http.Controllers.HttpControllerDescriptor(new System.Web.Http.HttpConfiguration(), ControllerType.Name, ControllerType);
                ILookup<string, System.Web.Http.Controllers.HttpActionDescriptor> ApiMappings = ApiControllerSelection.GetActionMapping(ApiDescriptor);

                foreach (var Maps in ApiMappings)
                {
                    foreach (System.Web.Http.Controllers.HttpActionDescriptor Actions in Maps)
                    {
                        Items += "[ controller=" + ControllerType.Name + " action=" + ((System.Web.Http.Controllers.ReflectedHttpActionDescriptor)(Actions)).MethodInfo + "]";
                    }
                }
            }
        }
[ controller=CarController action=System.Net.Http.HttpResponseMessage Login()]
[ controller=CarController action=Void MyCustomGet()]
[ controller=CarController action=Void MyCustomPost()]
[ controller=GenericControllerApi action=System.Net.Http.HttpResponseMessage Login()]
[ controller=ValuesController action=System.String Get()]
[ controller=ValuesController action=System.String Get(Int32)]
[ controller=ValuesController action=Void Post(System.String)]
[ controller=ValuesController action=Void Put(Int32, System.String)]
[ controller=ValuesController action=Void Delete(Int32)]
[ controller=ODataMetadataController action=Microsoft.Data.Edm.IEdmModel GetMetadata()]
[ controller=ODataMetadataController action=Microsoft.Data.OData.ODataWorkspace GetServiceDocument()]