asp.net-mvc-3 – MVC:为此对象定义的无参数构造函数

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – MVC:为此对象定义的无参数构造函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下控制器定义
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 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,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

解决方法

创建控制器时,它在构造函数中需要一个称为IAccommodationService的参数.
如果没有,则无法创建控制器.

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

猜你在找的asp.Net相关文章