'Identify the all the action methods in a WebAPI application
Is it possible to list all the action methods along with return types and parameters, of a WebApi application.
I saw a similar question and asnwered as well, but were for MVC (Getting All Controllers and Actions names in C#). But I tried to do some changes to in webapi, but couldnt do it.
Solution 1:[1]
Below code will get you all required information.
var controlleractionlist = asm.GetTypes()
.Where(type => typeof(ApiController).IsAssignableFrom(type))
.SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
.Where(m => !m.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Any())
.Select(x => new { Controller = x.DeclaringType.Name, Action = x.Name,
ReturnType = x.ReturnType.Name,
Attributes = String.Join(",", x.GetCustomAttributes().Select(a => a.GetType().Name.Replace("Attribute", ""))),
ControllerRouteAttr = ((System.Web.Http.RoutePrefixAttribute)Attribute.GetCustomAttribute(x.DeclaringType, typeof(System.Web.Http.RoutePrefixAttribute)))?.Prefix,
MethodRouteAttr = ((System.Web.Http.RouteAttribute)Attribute.GetCustomAttribute(x, typeof(System.Web.Http.RouteAttribute)))?.Template,
params1 = String.Join(",", x.GetParameters().Select(a => a.Name)),
returnType = String.Join(",", x.GetParameters().Select(a => a.Name.GetType()))
})
.OrderBy(x => x.Controller).ThenBy(x => x.Action).ToList();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Panda1122 |
