I use standart NinjectMVC3 Bootstrapper installed in the App_Start folder.
My application class looks like:
public class MvcApplication : HttpApplication
{
static void RegisterRoutes(RouteCollection routes)
{
// ... routes here ...
}
public void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
// empty
}
}
I have only following bidding rules registed in NinjectMVC3:
Bind<IAccountsRepository>().To<AccountsRepository>();
this.BindFilter<GlobalAuthFilter>(FilterScope.Global, 0);
And my global filter:
public class GlobalAuthFilter : IAuthorizationFilter
{
readonly IAccountsRepository _accountsRepository;
public GlobalAuthFilter(IAccountsRepository accountsRepository)
{
_accountsRepository = accountsRepository;
}
public void OnAuthorization(AuthorizationContext context)
{
// Code here never reached. Why? What's wrong?
}
}
There are any controllers in my application. And I want to invoke OnAuthorization for every action calls for every controllers.
But my code dosn't work. Thanks.
It's not quite clear from your code where more specifically are you configuring your kernel. This should be done in the
RegisterServices
method of~/App_Start/NinjectMVC3.cs
:When you install the
Ninject.MVC3
NuGet package the body of this method will be empty and it is where you should either directly configure dependencies or define Ninject modules that you would import in this method:where you have defined the custom module: