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.

2
  • If your filter is registered as a "global" filter then by default all requests to all your controller actions will pass through your filter. Commented Jan 17, 2013 at 3: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. Commented Jan 17, 2013 at 3:35

1 Answer 1

12

Try

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

Or

actionExecutingContextInstance.ActionDescriptor.GetCustomAttributes(typeof(MyAttribute), false).Length > 0)  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.