6

Consider the following code:

public class MyAttribute : Attribute {  }

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

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

My question is, here, how do I check if the requested Controller has been adorned with my custom Attribute.

Babu James
  • 2,740
  • 4
  • 33
  • 50
  • If your filter is registered as a "global" filter then by default all requests to all your controller actions will pass through your filter. – Matt Jan 17 '13 at 03:07
  • 1
    @Matt, I understand that and that's what I want to do. But I would like to exclude some logic based on the Controller or Action. So, I thought it would be better to have an attribute set for such Action or Controller and check the same inside the filter method to exclude desired logic. – Babu James Jan 17 '13 at 03:35

1 Answers1

12

Try

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

Or

actionExecutingContextInstance.ActionDescriptor.GetCustomAttributes(typeof(MyAttribute), false).Length > 0)  
jcastrov
  • 68
  • 2
  • 9
ideafountain
  • 575
  • 5
  • 17