ASP.NET MVC WebApi: no constructor without parameters defined for this object

I have an ASP.NET MVC 4 application that I want to implement in the Work of Work module.

In my web project, I have:

IocConfig.cs

using System.Web.Http; using NinjectMVC.Data; using NinjectMVC.Data.Contracts; using Ninject; namespace NinjectMVC { public class IocConfig { public static void RegisterIoc(HttpConfiguration config) { var kernel = new StandardKernel(); // Ninject IoC // These registrations are "per instance request". // See http://blog.bobcravens.com/2010/03/ninject-life-cycle-management-or-scoping/ kernel.Bind<RepositoryFactories>().To<RepositoryFactories>() .InSingletonScope(); kernel.Bind<IRepositoryProvider>().To<RepositoryProvider>(); kernel.Bind<INinjectMVCUow>().To<NinjectMVCUow>(); // Tell WebApi how to use our Ninject IoC config.DependencyResolver = new NinjectDependencyResolver(kernel); } } } 

Global.asax

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace NinjectMVC { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); // Tell WebApi to use our custom Ioc (Ninject) IocConfig.RegisterIoc(GlobalConfiguration.Configuration); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); } } } 

PersonsController.cs

 using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using NinjectMVC.Data.Contracts; using NinjectMVC.Model; namespace NinjectMVC.Controllers { public class PersonsController : ApiControllerBase { public PersonsController(INinjectMVCUow uow) { Uow = uow; } #region OData Future: IQueryable<T> //[Queryable] // public IQueryable<Person> Get() #endregion // GET /api/persons public IEnumerable<Person> Get() { return Uow.Persons.GetAll() .OrderBy(p => p.FirstName); } // GET /api/persons/5 public Person Get(int id) { var person = Uow.Persons.GetById(id); if (person != null) return person; throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } // OData: GET /api/persons/?firstname=\'Hans\'' // With OData query syntax we would not need such methods // /api/persons/getbyfirstname?value=Joe1 [ActionName("getbyfirstname")] public Person GetByFirstName(string value) { var person = Uow.Persons.GetAll() .FirstOrDefault(p => p.FirstName.StartsWith(value)); if (person != null) return person; throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } // Update an existing person // PUT /api/persons/ public HttpResponseMessage Put(Person person) { Uow.Persons.Update(person); Uow.Commit(); return new HttpResponseMessage(HttpStatusCode.NoContent); } } } 

When I try to surf: http://www.domain.com/Persons/Get Im getting:

There is no constructor without parameters for this object.

Are there any things that I missed? I would appreciate any help.

Here is the project zip file for the best links:

http://filebin.ca/E6aoOkaUpbQ/NinjectMVC.zip

+8
asp.net-mvc asp.net-web-api unit-of-work ninject
source share
2 answers

Wep.API uses a different IDependencyResolver than the MVC framework.

When you use HttpConfiguration.DependencyResolver , it only works for ApiControllers. But your ApiControllerBase comes from Controller ...

So your ApiControllerBase should inherit from ApiController .

Change it in ApiBaseController.cs:

 public abstract class ApiControllerBase : ApiController { } 

If you want to embed dependencies in regular derived classes of Controller , you need to use (in your IocConfig):

 System.Web.Mvc.DependencyResolver.SetResolver(new NinjectMvcDependencyResolver(container)); 

Please note that in this case you cannot use your NinjectDependencyResolver , because it is for ApiControllers .

So you need another NinjectMvcDependencyResolver , which should implement System.Web.Mvc.IDependencyResolver .

 public class NinjectMvcDependencyResolver: NinjectDependencyScope, System.Web.Mvc.IDependencyResolver { private IKernel kernel; public NinjectDependencyResolver(IKernel kernel) : base(kernel) { this.kernel = kernel; } } 
+8
source share

I know this is an old thread, but it might be useful for someone. :) In my case, there is no DependencyResolver on NinjectWebCommon.cs . I followed this article and everything was in order.

 RegisterServices(kernel); GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel); return kernel; 
+1
source share

All Articles