Validate ASP.NET MVC for a custom attribute in a controller or action

Consider the following code:

public class MyAttribute : Attribute { } [MyAttribute] public class MyControlller : Controller { //... } 

Now that I have a Global Action Filter that gets me an ActionExecutingContext object.

My question is how can I check if the requested Controller decorated with my custom Attribute .

+6
source share
1 answer

Try

 actionExecutingContextInstance.Controller.GetType().GetCustomAttributes(typeof(MyAttribute), false).Length > 0) 

Or

 actionExecutingContextInstance.ActionDescriptor.GetCustomAttributes(typeof(MyAttribute), false).Length > 0) 
+9
source

All Articles