2

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

4
  • 2
    Read strathweb.com/2012/05/… Commented Jan 26, 2014 at 18:35
  • this article was written when web apis were still in beta and pre-release - is it even still relevant? Commented Jan 26, 2014 at 18:41
  • Maybe ninject has a package for web api integration, at least autofac has one Commented Jan 26, 2014 at 18:52
  • You'll need the Ninject.Web.WebApi package. Commented Jan 26, 2014 at 21:07

1 Answer 1

1

Install the proper Ninject Nuget package for WebAPI:

PM> Install-Package Ninject.Web.WebApi -Version 3.0.0.2
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I'm getting the following error while trying to install: Already referencing a newer version of 'Microsoft.AspNet.WebApi'.
ok. ended up downloading from pre-release - that did the trick. thanks.
pre-release also getting same issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.