Question

I am trying to use Ninject to bind an ActionFilter to a controller where the controller has a FilterAttribute, but cannot get the ActionFilters to fire.

My filters and attributes are defined as:

public class AuthorisationFilter : IAuthorizationFilter {...}

public class AuthoriseAttribute : AuthorizeAttribute {...}

I then have a controller with the Authorise attribute declared:

[Authorise]
public class HomeController : Controller {...}

No matter how I try to use a Ninject module to bind the filter to the controller...:

Kernel
  .BindFilter<AuthorisationFilter>(FilterScope.Controller, 0)
  .WhenControllerHas<AuthoriseAttribute>();

Kernel
  .BindFilter<AuthorisationFilter>(FilterScope.Controller, 0)
  .WhenControllerType<HomeController>();

Kernel
  .BindFilter<AuthorisationFilter>(FilterScope.Controller, 0);

... the filters just don't fire.

Registering the filters in the GlobalFilterCollection doesn't help. Is there some other place the filters are supposed to be registered?

Thanks

Was it helpful?

Solution

I figured it out - I'm not using the NinjectHttpApplication to extend my HttpApplication class, and so the DependencyResolver was not being set (I'm presuming this is set by Ninject if you do use their implementation).

So a simple instruction to set the DependecyResolver results in the custom ActionFilters being instantiated and fired:

DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top