Search code examples
c#angularjsasp.net-mvcninject.web.mvcasp.net-apicontroller

An error has occurred.Type 'my api name controller' does not have a default constructorSystem.ArgumentException at System.Linq.Expressions.Expression


I am trying to create an mvc project with angular.so i create an api controller as you can see here :

namespace MvcApplication3.Controllers
{
    public class TopicsController : ApiController
    {
        private IMessageBoardRepository _repo;
        public TopicsController(IMessageBoardRepository repo)
        {
            _repo = repo;
        }

        public IEnumerable<Topic> Get(bool includeReplies = false)
        {
            IQueryable<Topic> results;

            if (includeReplies == true)
            {
                results = _repo.GetTopicsIncludingReplies();
            }
            else
            {
                results = _repo.GetTopics();
            }

            var topics = results.OrderByDescending(t => t.Created)
                                .Take(25)
                                .ToList();

            return topics;
        }

        // I didn't show this, but this is common
        public HttpResponseMessage Get(int id, bool includeReplies = false)
        {
            IQueryable<Topic> results;

            if (includeReplies == true)
            {
                results = _repo.GetTopicsIncludingReplies();
            }
            else
            {
                results = _repo.GetTopics();
            }

            var topic = results.Where(t => t.Id == id).FirstOrDefault();

            if (topic != null) return Request.CreateResponse(HttpStatusCode.OK, topic);

            return Request.CreateResponse(HttpStatusCode.NotFound);
        }
}
}

I am using ninject 3.2.1.0 to bind my interface to its repository .

   private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<MessageBoardContext>().To<MessageBoardContext>().InRequestScope();
            kernel.Bind<IMessageBoardRepository>().To<MessageBoardRepository>().InRequestScope();
        }

But when i call this api with this url :http://localhost:53005/api/v1/topics i got this error :

    An error has occurred.Type 'MvcApplication3.Controllers.TopicsController' does not have a default constructorSystem.ArgumentException   at System.Linq.Expressions.Expression.New(Type type)
   at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)

Solution

  • Finally i should install WebApiContrib.IoC.Ninject nuget and add this resolver to the ninjectwebcommen file as you can see here :

       private static IKernel CreateKernel()
            {
                var kernel = new StandardKernel();
                try
                {
                    kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                    kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
    
                    RegisterServices(kernel);
                    GlobalConfiguration.Configuration.DependencyResolver =
           new NinjectResolver(kernel);
                    return kernel;
                }
                catch
                {
                    kernel.Dispose();
                    throw;
                }
            }