In what order are filters executed in asp.net mvc

Filters run in the following order: Authorization filters Action filters Response filters 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. This enumeration defines the … Read more

How to pass parameters to a custom ActionFilter in ASP.NET MVC 2?

This is a way to make this work. You have access to the ControllerContext and therefore Controller from the ActionFilter object. All you need to do is cast your controller to the type and you can access any public members. Given this controller: public GenesisController : Controller { [CheckLoggedIn()] public ActionResult Home(MemberData md) { return … Read more

Redirecting to specified controller and action in asp.net mvc action filter

Rather than getting a reference to HttpContent and redirecting directly in the ActionFilter you can set the Result of the filter context to be a RedirectToRouteResult. It’s a bit cleaner and better for testing. Like this: public override void OnActionExecuting(ActionExecutingContext filterContext) { if(something) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary {{ “Controller”, “YourController” }, { … Read more

Order of execution with multiple filters in web api

Some things to note here: Filters get executed in the following order for an action: Globally Defined Filters -> Controller-specific Filters -> Action-specific Filters. Authorization Filters -> Action Filters -> Exception Filters Now the problem that you seem to mention is related to having multiple filters of the same kind (ex: Multiple ActionFilterAttribute decorated on … Read more

Best place to set CurrentCulture for multilingual ASP.NET MVC web applications

I used a global ActionFilter for this, but recently I realized, that setting the current culture in the OnActionExecuting method is too late in some cases. For example, when model after POST request comes to the controller, ASP.NET MVC creates a metadata for model. It occurs before any actions get executed. As a result, DisplayName … Read more

Unity Inject dependencies into MVC filter class with parameters

As per the post Passive Attributes, the DI-friendly solution is to separate the AuthorizeAttribute into 2 parts: An attribute that contains no behavior to flag your controllers and action methods with. A DI-friendly class that implements IAuthorizationFilter and contains the desired behavior. For our purposes, we just inherit AuthorizeAttribute to take advantage of some of … Read more