質問

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

役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top