I've managed to get a list of all controllers and their respective actions within my project. I am now attempting to create a custom attribute to be used on each action where I can set properties such as the action description Eg. "This creates a user". This seems to work fine but now the question is: How would I retrieve the custom attributes on each action?
Below gets a list of all controllers and actions. I just need to get each actions custom attribute called AccessControl
var controlleractionlist = asm.GetTypes()
.Where(type => typeof(System.Web.Mvc.Controller).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, CustomAttr = x.DeclaringType.GetCustomAttributes(typeof(AccessControl), false).Cast<AccessControl>()})
.OrderBy(x => x.Controller).ThenBy(x => x.Action).ToList();
Example of a typical controller action
[HttpGet]
[AccessControl(Description="Creates a user")]
public ActionResult Index()
{
return View();
}
And finally my custom attribute class
public class AccessControl : AuthorizeAttribute
{
public string Description { get; set; }
}
Thank you