MVC 3 Dependency Injection with Ninject 2.2 + Global Action Filter

后端 未结 1 1857
南方客
南方客 2021-01-05 08:30

I am trying to use ASP.NET MVC 3 and Ninject 2.2 to inject a logger object into a custom ActionFilterAttribute.

I am able to get this to work if I mark each control

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 09:17

    You might find the following blog post useful. So in your RegisterServices method simply:

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind().To();
        kernel.BindFilter(FilterScope.Global, 0);
    }       
    

    and to bring the BindFilter extension method into scope don't forget to add a proper using directive:

    using Ninject.Web.Mvc.FilterBindingSyntax;
    

    0 讨论(0)
提交回复
热议问题