8

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 debug on return Home() and step through then I bypass the action filter and go straight to the Home() method.

Am I declaring the action filter correctly?

10
  • I've recreated the scenario exactly as you have it above and my action filter is hit. I realize this information does nothing to solve your problem, but I thought you should know that your code is correct. Some other form of configuration may be causing the issue you're experiencing. Commented Nov 10, 2011 at 14:32
  • Thanks Nathan. Should I declare the class with the name TheFilterAttribute or with the name TheFilter? I thought this may be a problem and tried that. However same result. It does not step into the OnActionExecuted Commented Nov 10, 2011 at 14:39
  • Just tried it without the 'Attribute' on the end of my action filter; it doesn't seem to matter. Commented Nov 10, 2011 at 14:40
  • I did try that but no difference. Still trying to find a solution. I step line by line from the line before return Home() and it completely avoids the filter. Commented Nov 10, 2011 at 14:46
  • Can you try adding an override of OnActionExecuting and put a breakpoint in it? I'm curious if it gets hit. Commented Nov 10, 2011 at 14:46

4 Answers 4

31

Make sure you're implementing

System.Web.Mvc.ActionFilterAttribute 

and not

System.Web.Http.Filters.ActionFilterAttribute 

They both have OnActionExecuting and OnActionExecuted Methods, so it can be a little deceiving.

Sign up to request clarification or add additional context in comments.

Comments

2

Maybe you don't reach the method directly but call Edit action from other action?
put the filter on the controller and see what happens.

1 Comment

I am trying to apply a custom action filter on an action but it is not getting fired but when putting it above the controller it is can u help me out here?
1

I was also facing the same issue I was just missing the override keyword before the OnExecuting method. It started working after adding override keyword.

Comments

-3

Use the Onexecuting not onExecuted

 public override void OnActionExecuting(ActionExecutingContext filterContext) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.