How to use Ninject with ASP.NET Web API?

Blazi picture Blazi · Dec 24, 2012 · Viewed 21.1k times · Source

In MVC I simply make the class NinjectControllerFactory that implements DefaultControllerFactory interface then do some bindings in it. at last in Global I run it:

ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());

But what about using Ninject in ASP.NET Web API? there are some information on the web but are out dated and for pre-released versions.

Is there a straightforward way for this problem?

Answer

Mark Jones picture Mark Jones · Dec 24, 2012

The reason a lot of the articles are old is because the approach hasn't changed since June 2012 (RC released May 31st). You need to add the Ninject MVC3 Nuget package, then implement 'IDepenencyResolver' and then register your implementation.

The best two walk-thoughs are:

  1. http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/
  2. http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/