views:

1237

answers:

2

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?

A: 

The ActionFilterAttribute class has a property called "Order" which you can use to set the order in which the Action Filters are executed.
In your case you have to set the order of the Filter Attribute in the BaseController to 2 and the Filter Attribute in the DerivedController to 1:

[MyFilter(Order=2)]
public class BaseController:Controller
{

  public ActionResult MyAction() {

  }

}

[MySecondFilter(Order=1)]
public class DerivedController:BaseController
{

  public ActionResult AnotherAction() {

  }

}

Read this for more infos: http://msdn.microsoft.com/en-us/library/dd381609.aspx

Note: I didn't test this.

Marwan Aouida
+3  A: 

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()
GuyIncognito
Thanks. And yes, putting the logic into an ActionFilter on the baseclass will solve the problem and even result in a cleaner design.
Christian Dalager