1

i have a simple controller where i am using the interfaec like this ,

public class HomeController : Controller
{
    // GET: Home
    private IHotelService hotelService;
    public HomeController(IHotelService _hotelService)
    {
        hotelService = _hotelService;
    }
}

its working fine, but when i use same thing with API controller like

public class RoomController : BaseApiController
{
    private IHotelService hotelService;
    public RoomController(IHotelService _hotelService)
    {
        hotelService = _hotelService;
    }

it gives me error

6
  • 1
    Please post the error message or log here Commented Feb 3, 2018 at 8:27
  • An error occurred when trying to create a controller of type 'RoomController'. Make sure that the controller has a parameterless public constructor. Commented Feb 3, 2018 at 8:29
  • If you read here then you can understand that why that error message is msdn.microsoft.com/en-us/library/… Create a constructor without any parameter. Commented Feb 3, 2018 at 8:31
  • How are your injecting the service? What DI framework are you using? Commented Feb 3, 2018 at 8:36
  • i am using ninject web common for DI , and its working fine with normal web controller but not with api controller that is the actual problem Commented Feb 3, 2018 at 9:45

1 Answer 1

2

As pointed out here (and in several other answers on SO), you have most likely not registered your DI container with Web API. Web API is a separate framework than MVC and therefore it has a separate configuration, including dependency injection.

So, you need to set

GlobalConfiguration.Configuration.DependencyResolver = MyDependencyResovler(container);

at application startup. The details of how to do this depend on what container you are actually using and whether you use a stock dependency resolver or roll your own as shown in Dependency Injection in ASP.NET Web API 2.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.