MVC:没有为此对象定义的无参数构造函数

Sac*_*nth 14 razor asp.net-mvc-3

我有以下控制器定义

public class FeaturedAccommodationController : Controller
{
   ...

   public FeaturedAccommodationController(IAccommodationService accommodationService)
   {
      ...
   }

   public ActionResult Index(int page = 1)
   {
      ...
   }
   ...
}
Run Code Online (Sandbox Code Playgroud)

我有一个链接,我用它来调用这个索引操作方法

@Html.ActionLink("Featured Accommodations", "Index", "FeaturedAccommodation")
Run Code Online (Sandbox Code Playgroud)

出于某种原因,当我点击此链接时,我收到以下错误.这里有什么问题?

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) +98
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +67

[InvalidOperationException: An error occurred when trying to create a controller of type 'JattCore.UI.Admin.Controllers.FeaturedAccommodationController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +232
   System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49
   System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970356
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Run Code Online (Sandbox Code Playgroud)

小智 12

在创建控制器时,它在构造函数中需要一个名为的参数IAccommodationService.
如果不这样做,则无法创建控制器.

解决该问题的一种方法是使用依赖注入(DI)/控制反转(IoC),如ninject或类似的.
如果你有一个,那么记得注册IAccommodationService...

  • 解决此问题的常见方法是:如果创建自定义构造函数(带参数),则还必须定义不带参数的构造函数.C#编译器在后台自动创建一个构造函数,用于您没有定义构造函数的任何类,但是一旦指定了构造函数,它就不再这样做了. (2认同)

Shy*_*yju 9

添加一个constructor没有参数的控制器

public class FeaturedAccommodationController : Controller
{
   public FeaturedAccommodationController ()
   {
   }
   // your other parameterized constructors and action methods here
}
Run Code Online (Sandbox Code Playgroud)


Tre*_*ley 5

问题正是错误消息所说的,没有默认的无参数构造函数(例如):

public FeaturedAccommodationController()
{
}
Run Code Online (Sandbox Code Playgroud)

MVC运行时无法实例化您的控制器,因为它无法提供实现IAccommodationService.你需要像这样做穷人的构造函数注入:

public FeaturedAccommodationController()
    : this(new AccommodationService())
{
}

public FeaturedAccommodationController(IAccommodationService accommodationService)
{
}
Run Code Online (Sandbox Code Playgroud)

或使用IOC容器(如Unity,Ninject,Windsor等),它可以构建您的控制器并解决它们的依赖关系.