Home > ASP.NET MVC, C#, Ninject > Using ASP.NET filters with the Ninject Depencency Resolver

Using ASP.NET filters with the Ninject Depencency Resolver

Recently, I had an issue activating authorization in an ASP.NET MVC application. No matter what I tried to do, applying the AuthorizeAttribute to my controller methods had no effect. But in a dummy MvcApplication the AuthorizationAttribute worked without issues. After some debugging, I discovered that this was caused by the NinjectDependencyResolver I used to hook Ninject into the ASP.NET MVC stack.

An IDependencyResolver instance is responsible for resolving of all the required components of the ASP.NET MVC stack; it does a lot more than only resolving controllers. At a given moment during then processing of a request, ASP.NET MVC asks the dependency resolver to get all the IFilterProvider instances. Because I had not bound any filter provider to my Ninject kernel, Ninject returned an empty collection. This caused ASP.NET MVC not to use the default filter provider but to use no filter provider at all. Once I detected this behavior, the solution was very simple; I just had to register the default attribute based filter provider in my Ninject kernel.


If you only want to use Ninject (or any other IoC container) for resolving controllers, another solution could be not to use an IDependencyResolver but to use an IControllerFactory to hook the IoC container into the ASP.NET MVC stack. This way, the IoC container will only create controller instances and will not interfere with the rest of the ASP.NET MVC stack. Allowing you to use all the ASP.NET MVC defaults.

Categories: ASP.NET MVC, C#, Ninject
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: