簡體   English   中英

沒有為MVC Controller的此對象定義無參數構造函數

[英]No parameterless constructor defined for this object for MVC Controller

我有這個控制器,你可以看到:

namespace Web.Front.Controllers
{
    public class CarController : Controller
    {
        ICarService _CarService;
        public  CarController(ICarService carService)
        {
            _CarService = carService;
        }

        // GET: Car

        public ActionResult Index()
        {

            return View();
        }
    }
}

當我調用索引操作時,我收到此錯誤:

 No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +119
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
   System.Activator.CreateInstance(Type type) +11
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55

[InvalidOperationException: An error occurred when trying to create a controller of type 'Web.Front.Controllers.CarController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +178
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +102
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +184
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159

作為一個說明,我使用ninject向控制器注入服務。當我從構造函數中刪除服務時,它工作。

ninject代碼:

  private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<DidbaanContext>().To<DidbaanContext>();
        kernel.Bind<ICarService>().To<CarService>();
        kernel.Bind<ICarRepository>().To<CarRepository>();

    }     

mvc需要無參數的構造。 如果沒有構造函數,.Net-runtime會自動創建無參數構造,否則如果你有一個帶參數的構造,那么運行時就不會這樣做並引發錯誤。

以太在每個控制器上創建一個新的carService,或者使用serviceProvider。 unittest很簡單,有一個serviceprovider。 使用asp.net核心,在這里輕松輸入鏈接描述 ,如果你使用mvc 4或5.你可以在這里看到一個示例在這里輸入鏈接描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM