我尝试使用此代码,但它不起作用(返回空列表)
var controllerDescriptor = new ReflectedControllerDescriptor(controller);
ActionDescriptor[] actions = controllerDescriptor.GetCanonicalActions();
答案 0 :(得分:2)
找出了如何获取ApiController方法签名而不是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 + "]";
}
}
}
}
输出:
[ 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()]
我没有创建额外的控制器“ODataMetadataController”。可能由最初的项目创建添加。我也没想过看看这是否可以简化。