I want to apply an ActionFilter in ASP.NET MVC to EVERY action I have in my application - on every controller.
Is there a way to do this without applying it to every single ActionResult method ?
Yes, you can do this but it's not the way it works out of the box. I did the following:
Here's a sample of the action filter attribute:
public class SetCultureAttribute : FilterAttribute, IActionFilter
{
#region IActionFilter implementation
public void OnActionExecuted(ActionExecutedContext filterContext)
{
//logic goes here
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
//or logic goes here
}
#endregion IActionFilter implementation
}
Here's a sample of the base controller class with this attribute:
[SetCulture]
public class ControllerBase : Controller
{
...
}
Using this method as long as your controller classes inherits from ControllerBase then the SetCulture action filter would always be executed. I have a full sample and post on this on my blog if you'd like a bit more detail.
Hope that helps!
How things get better...2 years later we have
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorElmahAttribute());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With