ODataMetadataController

ODataMetadataController

本文介绍了我怎样才能得到ApiController的所有操作的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用这个code,但它不工作(返回空列表)

  VAR controllerDescriptor =新ReflectedControllerDescriptor(控制器);
ActionDescriptor [] =行动controllerDescriptor.GetCanonicalActions();


解决方案

摸索出如何获得ApiController方法签名,而不是控制。

 字符串项=;
        IEnumerable的<大会> 。组件= BuildManager.GetReferencedAssemblies()演员LT;大会>();        的foreach(大会FoundAssembly的组件)
        {
            字符串的AssemblyName = FoundAssembly.FullName;
            IEnumerable的<&所属类别GT;类型= FoundAssembly.DefinedTypes.Where(类型=>类型= NULL&放大器;!&安培; type.IsPublic和放大器;&安培; type.IsClass和放大器;&安培;!type.IsAbstract和放大器;&安培; typeof运算(ApiController).IsAssignableFrom(类型));
            的foreach(所属类别ControllerType的类型)
            {
                System.Web.Http.Controllers.ApiControllerActionSelector ApiControllerSelection =新System.Web.Http.Controllers.ApiControllerActionSelector();
                System.Web.Http.Controllers.HttpControllerDescriptor ApiDescriptor =新System.Web.Http.Controllers.HttpControllerDescriptor(新System.Web.Http.HttpConfiguration(),ControllerType.Name,ControllerType);
                ILookup<字符串,System.Web.Http.Controllers.HttpActionDescriptor> ApiMappings = ApiControllerSelection.GetActionMapping(ApiDescriptor);                的foreach(VAR地图在ApiMappings)
                {
                    的foreach(在地图System.Web.Http.Controllers.HttpActionDescriptor操作)
                    {
                        项目+ =[控制器=+ ControllerType.Name +行动=+((System.Web.Http.Controllers.ReflectedHttpActionDescriptor)(动作))的MethodInfo +];
                    }
                }
            }
        }

输出:

  [控制器= CarController行动= System.Net.Http.Htt presponseMessage登录()]
[控制器= CarController行动=虚空MyCustomGet()]
[控制器= CarController行动=虚空MyCustomPost()]
[控制器= GenericControllerApi行动= System.Net.Http.Htt presponseMessage登录()]
[控制器= Values​​Controller行动= System.String获取()]
[控制器= Values​​Controller行动= System.String获取(Int32)已]
[控制器= Values​​Controller行动=虚空邮政(System.String)
[控制器= Values​​Controller行动=虚空认沽(的Int32,System.String)
[控制器= Values​​Controller行动=虚空删除(Int32)已]
[控制器= ODataMetadataController行动= Microsoft.Data.Edm.IEdmModel的getMetaData()]
[控制器= ODataMetadataController行动= Microsoft.Data.OData.ODataWorkspace GetServiceDocument()]

有一个额外的控制器ODataMetadataController我没有创造。由最初的创建项目可能增加。我还没有看过,看是否可以简化。

I tried to use this code, but it does not work (return empty list)

var controllerDescriptor = new ReflectedControllerDescriptor(controller);
ActionDescriptor[] actions = controllerDescriptor.GetCanonicalActions();
解决方案

Worked out how to get the ApiController method signatures instead of Controller.

        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 + "]";
                    }
                }
            }
        }

Output:

[ 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()]

There is an extra controller "ODataMetadataController" that I did not create. Possibly added by the initial project creation. I also have not looked to see if this could be simplified.

这篇关于我怎样才能得到ApiController的所有操作的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 06:39