Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeginRequest-like filter in MVC 3?

I have some code in my application that I need to execute on every request, before anything else executes (even before authentication). So far I've been using the Application_BeginRequest event in my Global.asax, and this has worked fine. But this code needs to hit the database, and doing this from Global.asax doesn't feel right for some reason. Also, the Ninject.MVC3 nuget I'm using won't inject dependencies into my HttpApplication ctor.

So what I decided to do is move this code into its own global action filter. The problem I'm having now is that no matter what Order or FilterScope I give this filter, I can't get it to execute first; my authorization filter always beats it. MSDN seems to confirm this:

Filter Order

Filters run in the following order:

  1. Authorization filters
  2. Action filters
  3. Response filters
  4. Exception filters

For example, authorization filters run first and exception filters run last. Within each filter type, the Order value specifies the run order. Within each filter type and order, the Scope enumeration value specifies the order for filters.

I know I can use an HttpModule, but that doesn't feel very MVCish, so I am trying to exhaust all possiblities before going that route, which leads to my question:

Is there a BeginRequest equivalent for global action filters?

like image 380
Daniel Liuzzi Avatar asked Feb 02 '11 20:02

Daniel Liuzzi


People also ask

Which filter will execute last in MVC?

Exception Filters − Exception filters are the last type of filter to run. You can use an exception filter to handle errors raised by either your controller actions or controller action results.

Can we create custom filter in MVC?

You can create custom filter attributes by implementing an appropriate filter interface for which you want to create a custom filter and derive the FilterAttribute class to use that class as an attribute. For example, implement IExceptionFilter and the FilterAttribute class to create a custom exception filter.

Can we override filters in MVC?

ASP.NET MVC 5 has arrived with a very important feature called Filter Overrides. Using the Filter Overrides feature, we can exclude a specific action method or controller from the global filter or controller level filter. ASP.NET MVC 5 has arrived with a very important feature called Filter Overrides.


1 Answers

You could do this in the Initialize method of a base controller.

Another possibility is to register a global filter:

public class MyGlobalFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // that's gonna be hit
    }
}

and in the RegisterGlobalFilters event of your Global.asax:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new MyGlobalFilter());
}
like image 113
Darin Dimitrov Avatar answered Oct 21 '22 10:10

Darin Dimitrov