MVC 4 我有一个操作过滤器,我只想将其应用于基本控制器中的 Index 和 Edit 方法,但是当我将其应用于 Index 和 Edit 方法时,操作过滤器不会触发。但是,如果我在班级级别应用过滤器,它确实有效。
基本控制器:
//[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);
}
派生类调用基类:
public ActionResult Index()
{
ItemViewModel viewModel = _manageItemsAppServ.CreateNewViewModel(CurrentCompanyId);
viewModel.DDLOptions = _manageItemsAppServ.CreateFilterOptionsViewModel(CurrentCompanyId);
return base.IndexBase(viewModel, "Manage Materials");
}
动作过滤器:
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
}
}