我知道这是一个重复的问题,但我无法找到答案。 我正在尝试显示我的房间列表保存在数据库中,但我得到下一个错误:
Server Error in '/' Application.
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 'HotelProjectFinal.Controllers.RoomController'. 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() +8969412
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
但我有一个带有参数的构造函数:
namespace HotelProjectFinal.Controllers
{
public class RoomController : Controller
{
private IRoomRepository repository;
public RoomController(IRoomRepository roomRepository)
{
repository = roomRepository;
}
public ViewResult List()
{
return View(repository.Rooms);
}
}
}
我的观点是:
@model IEnumerable<HotelProjectFinal.Models.Room>
@{
ViewBag.Title = "List";
}
@foreach (var p in Model)
{
<div class="item">
<h3>@p.Room_number</h3>
@p.Room_Type
<h4>@p.Room_Type.Price.ToString("c")</h4>
</div>
}
我正在使用ninject:
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext,
Type controllerType)
{
return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
ninjectKernel.Bind<IRoomRepository>().To<EFRoomRepository>();
}
}
}
答案 0 :(得分:4)
你有一个控制器工厂,但堆栈跟踪没有说明Ninject的事实表明你忘了告诉MVC。
你可以通过添加一行告诉它来解决这个问题。
然而,推荐的做法是将Ninject挂钩到adding a NuGet reference to Ninject.MVC3。相关Ninject MVC3 wiki上有关于它的文档。
答案 1 :(得分:3)
请尝试将构造函数更改为以下内容:
public RoomController() { } // You were missing this parameterless constructor
[Inject]
public RoomController(IRoomRepository roomRepository)
{
repository = roomRepository;
}
Ninject正在寻找一个无参数构造函数,因为你没有在上面构造函数中指定你希望用于依赖注入的[Inject]。这混淆了“Ninject”并导致抛出异常。
主要DI模式是构造函数注入。当激活Ninject类型的实例时,将按顺序应用以下规则来选择要使用的类型构造函数之一: -
- 如果构造函数具有[Inject]属性,则使用它(但是如果将该属性应用于多个属性,Ninject将在检测时在运行时抛出NotSupportedException。)
- 如果没有构造函数具有[Inject]属性,Ninject将选择Ninject理解如何解析的参数最多的那个。
- 如果没有定义构造函数,Ninject将选择默认的无参数构造函数(假设有一个)。
可在此处找到更多信息:
https://github.com/ninject/ninject/wiki/Injection-Patterns
正如Ruben所指出的,[Inject]属性会在外部问题上污染控制器。
这会将您的代码绑定到特定容器。 (尽管Ninject允许自定义特定属性以寻找,但重点仍然是 - 您正在污染具有外部问题的接口。)
您的实际问题可能依赖于缺少对Ninject.MVC3的引用
答案 2 :(得分:2)
即使IRoomRepository 可以为null,也不会使它成为无参数构造函数。首先,看起来您的IoC没有正确连接到IRoomRepository。如果没有IoC或配置错误的IoC,控制器激活器会为您的控制器寻找无参数构造函数。
// This is a parameterless constructor.
public RoomController()
{ }
// This is not a parameterless constructor.
public RoomController(IRoomRepository roomRepository)
{
repository = roomRepository;
}
编辑,您使用的是Ninject.Mvc吗?您的基础MvcHttpApplication正在实施NinjectHttpApplication?