我正在测试Ninject,但是按照操作方法,我发现无法使其工作。网络上的信息非常混乱甚至是矛盾的。我正在使用Visual Studio 2012在MVC 4中开发一个网站,我确实使用Nuget安装了Ninject。
所以我得到一个错误:“没有为这个对象定义无参数构造函数。”一进入我的控制器。
我做了必要的步骤:
在我的家庭控制器中,我设置了这样的对象:
public ISurvey _survey { get; set; }
[Inject]
public HomeController(ISurvey s)
{
_survey = s;
}
我收到以下错误消息:
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, StackCrawlMark& stackMark) +113 System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232 System.Activator.CreateInstance(Type type, Boolean nonPublic) +83 System.Activator.CreateInstance(Type type) +6 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +110 [InvalidOperationException: An error occurred when trying to create a controller of type 'Surveys.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.] System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +247 System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +438 System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +226 System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +326 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +177 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
所以我想知道我做错了什么? 4个小时,我发现我的web.config文件可能有问题,或者它可能是缺少参考,或者可能是工厂缺失。现在我甚至不知道该怎么做。谢谢你的帮助。
答案 0 :(得分:1)
您还必须安装包含Ninject Controller Factory的Ninject.MVC3(https://www.nuget.org/packages/Ninject.MVC3/),它负责实例化控制器并将正确的依赖项注入控制器构造函数。我几乎可以肯定,如果你通过Nuget安装Ninject.MVC3,一切都会自动配置。
如果没有这个软件包,MVC会使用它的默认控制器工厂,它对注入依赖项一无所知。
答案 1 :(得分:0)
检查你的Web.config中是否有两行用于MVC版本控制,我有类似的东西:
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="4.0.0.0-4.0.0.1" newVersion="4.0.0.1" />
</dependentAssembly>
如果你这样做,那么用一行代替它们,以重定向到你所使用的最高版本4.0.0.1
:
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.1" newVersion="4.0.0.1" />
</dependentAssembly>
这对我有用。