Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install an action filter in all actions in ASP.NET MVC?

Is there a way to have an action filter, like

public class MyActionFilterAttribute : ActionFilterAttribute {
    public override void OnActionExecuting(ActionExecutingContext context) {
    ...

be automatically applied to all actions in a web site?

like image 824
pupeno Avatar asked Dec 02 '22 07:12

pupeno


1 Answers

I don't believe there is an out-of-the-box way to do this. The easiest thing to do for simple sites is just apply the filter at the Controller level. This is pretty common, and generally it's a good idea to have your own base controller class in case things like this crop up where you want to propagate it to all your controllers. E.g.:

[MyActionFilter]
public class MyBaseController : Controller
{
  ...
}

public class HomeController : MyBaseController
{
  ...
}

That being said, here is a blog post showing how you can achieve application wide action filters. Looks like a small amount of work, but perhaps you can use this technique.

like image 193
womp Avatar answered Dec 04 '22 11:12

womp