MVC 4
I have an action filter that I want applied only to the Index and Edit methods in my base controller, but the action filter does not fire when I apply it to the Index and Edit methods. It DOES work however if I apply the filter at the class level.
Base Controller:
//[SetAreaControllerFilter] <------ does fire here
public class BaseControllerSingle<TRepository, TViewModelSingle> : BaseController
[SetAreaControllerFilter] <----- doesn't fire here
public ActionResult IndexBase(TViewModelSingle viewModel, string pageTitle)
{
ViewBag.Header = pageTitle;
return View(viewModel);
}
Derived class call to base:
public ActionResult Index()
{
ItemViewModel viewModel = _manageItemsAppServ.CreateNewViewModel(CurrentCompanyId);
viewModel.DDLOptions = _manageItemsAppServ.CreateFilterOptionsViewModel(CurrentCompanyId);
return base.IndexBase(viewModel, "Manage Materials");
}
Action Filter:
public class SetAreaControllerFilterAttribute : ActionFilterAttribute, IActionFilter
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//Set variable to be used by view and partials
dynamic viewBag = filterContext.Controller.ViewBag;
viewBag.AreaName = (string)filterContext.RouteData.DataTokens["area"]; //used by OTIS.Core.js to create links
viewBag.ControllerName = (string)filterContext.RouteData.Values["controller"]; //used by Toolbar adn OTIS.Core.js to create links
}
}
Well it appears that there is NOT a way to apply a filter to an action in a base controller. You need to apply it at the class level or to the actions in the derived controller class. This does not seem like it should be this way, but no one has been offer a solution that works.