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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top