I started a new ASP.NET MVC project and I'm using Ninject as my IOC controller.

As far as regular MVC Controllers goes - everything work fine and binding is done to the controller constructor as expected.

I've added a WEB API to my project and did pretty much the same thing. here is the API constructor:

public class DetailsController : ApiController
{
    private IClientInfoRetriever _clientInfoRetriever;

    public DetailsController(IClientInfoRetriever clientInfoRetriever)
    {
        _clientInfoRetriever = clientInfoRetriever;
    }

    .
    .
    .
    // The rest of my methods
}

here is the Ninject binding:

Bind<IClientInfoRetriever>().To<ClientInfoRetriever>();

but when I try to access my API (just putting the URL in the browser for a get action) I get the following error:

An error occurred when trying to create a controller of type 'DetailsController'. Make sure that the controller has a parameterless public constructor.

It's expecting to get an empty constructor, but if I give it an empty constructor to use - it won't be initializing the object I need.

What am I doing wrong? does Ninject support web api?

Thanks

有帮助吗?

解决方案

Install the proper Ninject Nuget package for WebAPI:

PM> Install-Package Ninject.Web.WebApi -Version 3.0.0.2
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top