Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my action filter get 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 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?

like image 909
Samantha J T Star Avatar asked Nov 10 '11 14:11

Samantha J T Star


People also ask

Which action filter triggers first?

For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter. Authorization filters are used to implement authentication and authorization for controller actions.

How do I get an action filter model?

Go to the FilterConfig class to register the filter. That is all you need to do to create an action filter, register it and to alter the returned model. As an aside, if you want to alter the value of an incoming parameter you override the OnActionExecuting method of the action filter.

What is the order in which the action filters are executed?

Filters execute in this order: Authorization filters. Action filters. Response/Result filters.


1 Answers

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.

like image 134
Josh Noe Avatar answered Sep 20 '22 02:09

Josh Noe