MVC5, WebAPI2 and Ninject Constructor error without parameters

So I have the exact opposite problem: MVC5, Web API 2, and Ninject

I have a new MVC5 / WebAPI2 project that has both a "Controller" and an "ApiControllers".

I use the latest unstable version of Ninject.Web.WebAPI without changing the code for NinjectDependencyResolve.cs and Ninject.WebCommom.cs (in addition to binding to my dependency), the injection of the ApiController constructor works. However, when I call the MVC controller, I get:

There is no constructor without parameters for this object.

+4
dependency-injection asp.net-mvc-5 ninject asp.net-web-api2
Feb 11 '14 at 20:10
source share
1 answer

The problem is that dependency resolver is required for MVC and WebAPI. Depending on which set of Ninject libraries you use, you get only one of them connected to you.

i.e. if you use the Ninject.Web.WebAPI library, you need to manually install the MVC converter:

System.Web.Mvc.DependencyResolver.SetResolver(new NinjectResolver(kernel)); 

(I did it in NinjectWebCommon.cs CreateKernel() )

Your Ninject recognizer can inherit the interface for WebAPI and MVC:

 public class NinjectResolver : NinjectScope, System.Web.Http.Dependencies.IDependencyResolver, System.Web.Mvc.IDependencyResolver 
+4
Mar 07 '14 at 16:24
source share



All Articles