I created a new MVC 4 project. I created a custom action filter and inherited from ActionFilterAttribute. I created an override for OnActionExecuting(). At the moment, it doesn’t do much; it just outputs a message to the output window. Last, I registered the action filter in RegisterGlobalFilters(), found in FilterConfig.cs.
When I run the application the application, the breakpoint gets hit. This is good. However, I didn’t add any attributes to any controllers. Is this behavior correct? When do I need to add attributes to my controllers? How does this work?
RegisterGlobalFilters is applicable to all controllers.
You may want to add specific attributes to specific controllers in some cases. For example, a certain controller may require a certain role using
[Authorize (Roles="group1")]whereas all others do not. Adding the attribute to that controller only will give you this functionality.