我有以下控制器定义
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
答案 0 :(得分:12)
创建控制器时,它需要在构造函数中有一个名为IAccommodationService
的参数
如果不这样做,则无法创建控制器。
解决问题的一种方法是使用依赖注入(DI)/控制反转(IoC),如ninject或类似的。
如果您有,请记得注册IAccommodationService
...
答案 1 :(得分:9)
向控制器添加没有参数的constructor
public class FeaturedAccommodationController : Controller
{
public FeaturedAccommodationController ()
{
}
// your other parameterized constructors and action methods here
}
答案 2 :(得分:5)
问题正是错误消息所说的,没有默认的无参数构造函数(例如):
public FeaturedAccommodationController()
{
}
MVC运行时无法实例化您的控制器,因为它无法提供IAccommodationService
的实现。你需要像这样做穷人的构造函数注入:
public FeaturedAccommodationController()
: this(new AccommodationService())
{
}
public FeaturedAccommodationController(IAccommodationService accommodationService)
{
}
或使用IOC容器(如Unity,Ninject,Windsor等),它可以构建您的控制器并解决它们的依赖关系。
答案 3 :(得分:1)
您的问题几乎完全与此问题重复:
Windows Azure, MVC3, No parameterless constructor defined for this object
您的错误跟踪甚至指出了问题:
[InvalidOperationException:尝试创建类型为“JattCore.UI.Admin.Controllers.FeaturedAccommodationController”的控制器时发生错误。确保控制器具有无参数的公共构造函数。]
答案 4 :(得分:1)
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root
答案 5 :(得分:0)
您可以通过实施DefaultControllerFactory
并使用ControllerBuilder.Current.SetControllerFactory
答案 6 :(得分:0)
这可能发生的一个原因是因为这是您应用程序中的第一个也是唯一一个控制器。我的建议是有一个名为Home的默认控制器,没有构造函数(作为错误状态),然后添加另一个控制器 - 你提到的控制器,可以有一个构造函数。