为什么我的动作过滤器没有被调用?

Sam*_*tar 7 asp.net-mvc asp.net-mvc-3

代码:

public class TheFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
    }
}

public class NotesController : BaseController
{
    [TheFilter]
    [HttpPost]
    public ActionResult Edit(EditViewModel viewModel)
    {
        viewModel.Note.Modified = DateTime.Now;
        viewModel.Note.ModifiedBy = User.Identity.Name;
        var noteTable = StorageHelper.GetTable<Note>(viewModel.PageMeta.DataSourceID);
        noteTable.AddOrUpdate(viewModel.Note);
        return Home();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在返回Home()上调试并逐步执行时,我绕过动作过滤器并直接进入Home()方法.

我是否正确声明了动作过滤器?

Jos*_*Noe 30

确保你正在实施

System.Web.Mvc.ActionFilterAttribute
Run Code Online (Sandbox Code Playgroud)

并不是

System.Web.Http.Filters.ActionFilterAttribute
Run Code Online (Sandbox Code Playgroud)

他们都有OnActionExecuting和OnActionExecuted方法,所以它可能有点欺骗.


gdo*_*ica 2

也许您没有直接访问该方法,而是从其他操作调用编辑操作?
将过滤器放在控制器上,看看会发生什么。