समस्या "CommentsController एक डिफ़ॉल्ट निर्माता नहीं है"।त्रुटि
त्रुटि:
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)"}
मैं बस मुझे पता है आप देखना चाहते हैं कि क्या जाने के लिए आवश्यक किसी भी कोड देने के लिए खुश हूँ।
नियंत्रक:
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;
}
}
जावास्क्रिप्ट:
$(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);
});
});
});
क्या आपने डीआई कंटेनर को सही तरीके से स्थापित किया था, और इसे एप्लिकेशन स्टार्ट से लॉन्च किया था? क्या आपने इंजेक्ट करने के लिए ICommentRepository का एक उदाहरण कॉन्फ़िगर किया था? –
मैंने नहीं किया है। क्या यह उपयोगकर्ता एकता या Ninject के लिए बेहतर होगा? वे केवल दो ही हैं जिन्हें मैं उपयोग करने में रूचि रखता हूं, मैं आईओसी और डीआई की अवधारणा को समझता हूं लेकिन मैं इसे एमवीसी 4 और वेबएपीआई के साथ उपयोग करने के लिए सीखने की कोशिश कर रहा हूं ... क्या मैं इसे NuGet के माध्यम से जोड़ता हूं? –