Question

I have an MVC4 app with multiple controllers that all inherit from BaseController. In some of these controllers, I have Actions with a Custom Attribute of PageKey. Page Key has two properties (Group & Key).

I need a way to get every instance of this attribute in all controllers and access the values. I also want to not have to modify the code later when I add new controllers or instances of the attribute.

Was it helpful?

Solution

Ok, got it.

var attributes = Assembly.GetExecutingAssembly()
            .GetTypes()
            .SelectMany(t => t.GetMethods())
            .SelectMany(m => m.GetCustomAttributes())
            .Where(a => a.GetType() == typeof(PageKeyAttribute));

Thanks to Alexei Levenkov.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top