locked
No parameterless constructor defined for this object. RRS feed

  • Question

  • User456628452 posted

    i am using ninject with mvc 5,  i am getting this error 

    [InvalidOperationException: An error occurred when trying to create a controller of type 'Intranet.Controllers.StaffController'. Make sure that the controller has a parameterless public constructor.]

    This is the code i have written

    [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Intranet.App_Start.NinjectWebCommon), "Start")]
    [assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(Intranet.App_Start.NinjectWebCommon), "Stop")]
    
    namespace Intranet.App_Start
    {
        using System;
        using System.Web;
        using IntranetModels.Services;
        using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    
        using Ninject;
        using Ninject.Web.Common;
        using Ninject.Web.Common.WebHost;
    
        public static class NinjectWebCommon 
        {
            private static readonly Bootstrapper bootstrapper = new Bootstrapper();
    
            /// <summary>
            /// Starts the application
            /// </summary>
            public static void Start() 
            {
                DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
                DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
                bootstrapper.Initialize(CreateKernel);
            }
            
            /// <summary>
            /// Stops the application.
            /// </summary>
            public static void Stop()
            {
                bootstrapper.ShutDown();
            }
            
            /// <summary>
            /// Creates the kernel that will manage your application.
            /// </summary>
            /// <returns>The created kernel.</returns>
            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);
                    return kernel;
                }
                catch
                {
                    kernel.Dispose();
                    throw;
                }
            }
    
            /// <summary>
            /// Load your modules or register your services here!
            /// </summary>
            /// <param name="kernel">The kernel.</param>
            private static void RegisterServices(IKernel kernel)
            {
                kernel.Bind<IHrService>().To<HrService>().InRequestScope();
            }
    
           
        }
    }

    controller

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Mvc;
    using IntranetModels.Services;
    using IntranetModel.Models;
    
    namespace Intranet.Controllers
    {
        public class StaffController : Controller
        {
            private IntranetEntities db = new IntranetEntities();
    
            private IHrService hrService;
    
            public StaffController(
              IHrService _hrService
            )
            {
                this.hrService = _hrService;
            }
    
            [HttpGet]
            public async Task<ActionResult> StaffDirectory()
            {
                var OrgUnitList = await hrService.GetOrgUnitList().ConfigureAwait(false);
                ViewBag.OrgUnitList = new SelectList(OrgUnitList, "Id,Value");
                return View();
            }
    }
    }

    route config

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace Intranet
    {
        public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
            }
        }
    }
    

    Saturday, December 28, 2019 11:08 AM

Answers

All replies

  • User1535942433 posted

    Hi marya,

    Accroding to your description and codes,I suggest you could creat a  parameterless public constructor.
    Could you post more codes and runtime dependencies?It will help us solve your issue more faster.

    Best regards,

    Yijing Sun

    Tuesday, December 31, 2019 9:57 AM
  • User-1780421697 posted

    Did you register staff controller in DI container?

    Have a look at: https://hackernoon.com/ddd-5a1x3zl2

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 15, 2020 7:15 AM