Why is my action filter not called?

The code:

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(); } } 

When I am debugging when returning Home () and having stepped over, I will bypass the action filter and go directly to the Home () method.

Am I declaring an action filter correctly?

+6
source share
3 answers

You may not have reached the method directly, but are you invoking the "Edit" action from another action?
put the filter on the controller and see what happens.

+2
source

Make sure you implement

 System.Web.Mvc.ActionFilterAttribute 

but not

 System.Web.Http.Filters.ActionFilterAttribute 

They have OnActionExecuting and OnActionExecuted Methods, so it can trick a little.

+22
source

Use Onexecuting not onExecuted

  public override void OnActionExecuting(ActionExecutingContext filterContext) 
-3
source

Source: https://habr.com/ru/post/901216/


All Articles