14

我有以下控制器定义

public class FeaturedAccommodationController : Controller
{
   ...

   public FeaturedAccommodationController(IAccommodationService accommodationService)
   {
      ...
   }

   public ActionResult Index(int page = 1)
   {
      ...
   }
   ...
}

我有一个链接,用于调用此索引操作方法

@Html.ActionLink("Featured Accommodations", "Index", "FeaturedAccommodation")

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

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
4

7 回答 7

12

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

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

于 2012-09-26T15:44:46.963 回答
10

constructor向控制器添加不带参数的

public class FeaturedAccommodationController : Controller
{
   public FeaturedAccommodationController ()
   {
   }
   // your other parameterized constructors and action methods here
}
于 2012-09-26T15:45:13.120 回答
5

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

public FeaturedAccommodationController()
{
}

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

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

public FeaturedAccommodationController(IAccommodationService accommodationService)
{
}

或使用 IOC 容器(例如 Unity、Ninject、Windsor 等)来构建您的控制器并解决它们的依赖关系。

于 2012-09-26T15:47:06.893 回答
1

您的问题几乎与此问题完全相同:

Windows Azure,MVC3,没有为此对象定义无参数构造函数

您的错误跟踪甚至指向问题:

[InvalidOperationException:尝试创建类型为“JattCore.UI.Admin.Controllers.FeaturedAccommodationController”的控制器时发生错误。确保控制器有一个无参数的公共构造函数。]

于 2012-09-26T15:45:06.777 回答
1
  • 停止 IIS
  • 删除C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root
  • 启动 IIS
于 2015-01-24T12:29:04.083 回答
0

DefaultControllerFactory您可以通过实现和设置来使用ASP.NET MVC 的内置功能ControllerBuilder.Current.SetControllerFactory

于 2014-03-10T19:57:54.223 回答
0

这可能发生的一个原因是因为这是您的应用程序中的第一个也是唯一一个控制器。我的建议是有一个名为 Home 的默认控制器,没有构造函数(如错误状态),然后添加另一个控制器 - 你提到的那个,它可以有一个构造函数。

于 2016-02-03T23:58:08.950 回答