Babu James
Babu James

Reputation: 2843

ASP.NET MVC check for custom Attribute in Controller or Action

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.

Upvotes: 6

Views: 5856

Answers (1)

ideafountain
ideafountain

Reputation: 575

Try

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

Or

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

Upvotes: 12

Related Questions