I have several Web API controllers in my project. After a lot of redundant code, I've refactored them into the code below, which seemed to be highly reusable. However, I'm suddenly getting the error Make sure that the controller has a parameterless public constructor
, which seems to be caused by Ninject not being able to resolve the controller bindings. I'm not sure how to bind them.
My code:
public interface IController<T, TK>
{
DataSourceResult Get(DataSourceRequest request);
T Get(TK id);
HttpResponseMessage Post(T model);
T Put(T model);
TK Delete(TK id);
}
public abstract class BaseController<T, TK> : ApiController, IController<T, TK>
{
private readonly IRepository<T, TK> repository;
public BaseController(IRepository<T, TK> repository)
{
this.repository = repository;
}
/* methods here */
}
public class ReceiptsController : BaseController<ReceiptViewModel, long>
{
public ReceiptsController(IRepository<ReceiptViewModel, long> repository) :
base(repository)
{
}
}
In the ninject RegisterServices
method, I've tried the following:
kernel.Bind<IController<OntvangstViewModel, long>>().To<OntvangstenController>();
kernel.Bind<BaseController<OntvangstViewModel, long>>().To<OntvangstenController>();
But neither seem to work. Is my implementation or inheritance wrong? Or should I bind them differently?