문제 : MVC4 WebAPI를 사용 중이고 Get() 호출 중에 오류가 발생합니다.오류 "CommentsController 기본 생성자가 없습니다"
오류 :
System.ArgumentException: Type 'Comments2.Controllers.CommentsController' does not have a default constructor
스택 트레이스 :
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)"}
나는 단지 내가 당신이보고 싶어요 알려 필요한 코드를 제공 드리겠습니다.
컨트롤러 :
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 comment;
if (!repository.TryGet(id, out comment))
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
return comment;
}
}
자바 스크립트 : 당신은 의존성 주입 작동하지 않습니다 HttpControllerActivator의 기본 구현을 사용하는 것처럼
$(function() {
$("#getComments").click(function() {
// We're using a Knockout model. This clears out the existing comments.
viewModel.comments([]);
$.get('/api/comments', function (data) {
// Update the Knockout model (and thus the UI) with the comments received back
// from the Web API call.
viewModel.comments(data);
});
});
});
DI 컨테이너를 올바르게 설정하고 응용 프로그램 시작에서 시작 했습니까? 삽입 할 ICommentRepository의 인스턴스를 구성 했습니까? –
나는 그렇지 않다. Unity 또는 Ninject를 사용하는 것이 더 좋을까요? 그것들은 제가 사용하고자하는 유일한 두 가지입니다, IoC와 DI의 개념을 이해 합니다만 MVC4와 WebAPI와 함께 사용하는 법을 배우려고합니다 ... 그냥 NuGet을 통해 추가합니까? –