Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 415
Babu James Avatar asked Jan 17 '13 02:01

Babu James


1 Answers

Try

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

Or

actionExecutingContextInstance.ActionDescriptor.GetCustomAttributes(typeof(MyAttribute), false).Length > 0)  
like image 161
ideafountain Avatar answered Nov 20 '22 20:11

ideafountain