Problem: I'm using MVC4 WebAPI and am throwing an error during a Get() call.
Error:
System.ArgumentException: Type 'Comments2.Controllers.CommentsController' does not have a default constructor
StackTrace:
at System.Linq.Expressions.Expression.New(Type type)
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}
I'm happy to give any code required simply let me know what you'd like to see.
Controller:
namespace Comments2.Controllers
{
//[Authorize]
public class CommentsController : ApiController
{
ICommentRepository repository;
public CommentsController(ICommentRepository repository)
{
this.repository = repository;
}
[Queryable]
public IQueryable<Comment> GetComments()
{
return repository.Get().AsQueryable();
}
public Comment GetComment(int id)
{
Comment ment;
if (!repository.TryGet(id, out ment))
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
return ment;
}
}
JavaScript:
$(function() {
$("#getComments").click(function () {
// We're using a Knockout model. This clears out the existing ments.
viewModelments([]);
$.get('/api/ments', function (data) {
// Update the Knockout model (and thus the UI) with the ments received back
// from the Web API call.
viewModelments(data);
});
});
});
Problem: I'm using MVC4 WebAPI and am throwing an error during a Get() call.
Error:
System.ArgumentException: Type 'Comments2.Controllers.CommentsController' does not have a default constructor
StackTrace:
at System.Linq.Expressions.Expression.New(Type type)
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}
I'm happy to give any code required simply let me know what you'd like to see.
Controller:
namespace Comments2.Controllers
{
//[Authorize]
public class CommentsController : ApiController
{
ICommentRepository repository;
public CommentsController(ICommentRepository repository)
{
this.repository = repository;
}
[Queryable]
public IQueryable<Comment> GetComments()
{
return repository.Get().AsQueryable();
}
public Comment GetComment(int id)
{
Comment ment;
if (!repository.TryGet(id, out ment))
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
return ment;
}
}
JavaScript:
$(function() {
$("#getComments").click(function () {
// We're using a Knockout model. This clears out the existing ments.
viewModel.ments([]);
$.get('/api/ments', function (data) {
// Update the Knockout model (and thus the UI) with the ments received back
// from the Web API call.
viewModel.ments(data);
});
});
});
Share
Improve this question
edited May 26, 2013 at 17:15
abatishchev
100k88 gold badges301 silver badges442 bronze badges
asked Jul 15, 2012 at 21:32
Computer GuyComputer Guy
4112 gold badges9 silver badges21 bronze badges
2
- 1 Did you properly set up a DI container, and launched it from the application start? Did you configure an instance of ICommentRepository to inject? – Leon Cullens Commented Jul 15, 2012 at 21:39
- I have not. Would it be better to user Unity or Ninject? Those are the only two I'm interested in using, I understand the concept of IoC and DI but I'm trying to learn to use it with MVC4 and WebAPI ...do I just add that via NuGet? – Computer Guy Commented Jul 15, 2012 at 21:47
2 Answers
Reset to default 8It seams like you are using default implementation of HttpControllerActivator which will not work with dependency injection. Try this it integrates unity container to handle dependency but you can modify it to use any implementation of DI you want.
I'm not sure what IOC Container you are using, I personally use Ninject and here are the instructions I use to get this working properly.