Action filter constructor being called repeatedly for single controller

空扰寡人 提交于 2019-12-25 04:58:17

问题


I'm using an action filter on some of my controllers, irrelevant what it does, that uses Ninject for dependency injection. I set breakpoints in the controller constructor and the action filter constructor, and I found that for a single request, the controller's constructor gets called once and the action filter's 8 times. The following code produces the effect, virtually the same as that given in this answer.

Attribute:

public class NotificationAttribute : Attribute { }

public class NotificationActionFilter : IActionFilter
{
    private IUnitOfWork _unitOfWork;

    public NotificationActionFilter(IUnitOfWork uow)
    {
        _unitOfWork = uow;
    }

    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        // do stuff
    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {

    }
}

Binding:

kernel.BindFilter<NotificationActionFilter>(FilterScope.Controller, 0).WhenControllerHas<NotificationAttribute>();

Controller:

[NotificationAttribute]
public class TestController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

It's always exactly 8, but I have no idea why. I've really been pulling my hair out over this one, just hoping someone might have dealt with something similar before.

来源:https://stackoverflow.com/questions/11333575/action-filter-constructor-being-called-repeatedly-for-single-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!