Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling FilterAttribute's OnActionExecuting before BaseController's OnActionExecuting

I have a BaseController in which I put in some data in the ViewData collection by overriding OnActionExecuting.

Now i have an Action in a ChildController that doesn't need that view data.

For that purpose I created an DontPopulateViewData ActionFilterAttribute that sets a bool on the BaseController that prevents the BaseController from populating the viewdata.

Problem: the ActionFilters OnActionExecuting method is called after the one in BaseController and not before.

Will ActionFilters always be called before overridden OnActionExecuting in base controllers and is there a way to get around this?

like image 486
Christian Dalager Avatar asked May 19 '09 13:05

Christian Dalager


People also ask

How do you call OnActionExecuting?

OnActionExecuting: It is called just before the action method is going to call. OnActionExecuted: It is called just after the action method is called. OnResultExecuting: It is called just before the result is executed; it means before rendering the view.

Which action filter triggers first?

Filters are executed in the order listed above. For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter. Authorization filters are used to implement authentication and authorization for controller actions.

How do I redirect OnActionExecuting in base controller?

4 Answers. Show activity on this post. public override void OnActionExecuting(ActionExecutingContext filterContext) { ... if (needToRedirect) { ... filterContext. Result = new RedirectResult(url); return; } ... }

What is an ActionFilter?

Action filters are used to implement the logic that get executed before or after a controller action executes. Authorization Filters. It is used to implement authorization and authentication for action filters. Result Filters. Result filters contains logic that gets executed before or after a view result gets executed.


1 Answers

In addition to what Marwan Aouida posted and suggested (using an ActionFilter on the base class), I don't think you're going to be able to create an ActionFilter that executes before the OnActionExecuting() overload on the base class. The following code:

 [MyActionFilter(Name = "Base", Order = 2)]
 public class MyBaseController : Controller
 {
     protected override void OnActionExecuting(ActionExecutingContext filterContext)
     {
        Response.Write("MyBaseController::OnActionExecuting()<br>");
        base.OnActionExecuting(filterContext);
     }

     protected override void Execute(System.Web.Routing.RequestContext requestContext)
     {
        requestContext.HttpContext.Response.Write("MyBaseController::Execute()<br>");
        base.Execute(requestContext);
     }

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        Response.Write("MyBaseController::OnActionExecuted()<br>");
        base.OnActionExecuted(filterContext);
    }
 }

 public class MyActionFilter : ActionFilterAttribute
 {
    public string Name;

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        filterContext.HttpContext.Response.Write("MyActionFilter_" + Name + "::OnActionExecuted()<br>");
        base.OnActionExecuted(filterContext);
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Write("MyActionFilter_" + Name + "::OnActionExecuting()<br>");
        base.OnActionExecuting(filterContext);
    }
 }

 public class MyTestController : MyBaseController
 {
     [MyActionFilter(Name = "Derived", Order = 1)]
     public void Index()
     {
        Response.Write("MyTestController::Index()<br>");
     }
 }

produces this output:

MyBaseController::Execute()
MyBaseController::OnActionExecuting()
MyActionFilter_Derived::OnActionExecuting()
MyActionFilter_Base::OnActionExecuting()
MyTestController::Index()
MyActionFilter_Base::OnActionExecuted()
MyActionFilter_Derived::OnActionExecuted()
MyBaseController::OnActionExecuted()
like image 162
GuyIncognito Avatar answered Sep 18 '22 22:09

GuyIncognito