Dav*_*ave 5 ninject ninject.web.mvc
我正在测试Ninject,但是按照操作方法,我发现无法使其工作.网络上的信息非常混乱甚至是矛盾的.我正在使用Visual Studio 2012在MVC 4中开发一个网站,我确实使用Nuget安装了Ninject.
所以我得到一个错误:"没有为这个对象定义无参数构造函数." 一进入我的控制器.
我做了必要的步骤:
在我的家庭控制器中,我设置了这样的对象:
public ISurvey _survey { get; set; }
[Inject]
public HomeController(ISurvey s)
{
_survey = s;
}
Run Code Online (Sandbox Code Playgroud)我收到以下错误消息:
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文件可能有问题,或者它可能是缺少参考,或者可能是工厂缺失.现在我甚至不知道该怎么做.谢谢你的帮助.
您还必须安装 Ninject.MVC3 ( https://www.nuget.org/packages/Ninject.MVC3/ ),其中包括 Ninject 控制器工厂,负责实例化控制器并将正确的依赖项注入控制器构造函数。我几乎可以肯定,如果您通过 Nuget 安装 Ninject.MVC3,所有内容都会自动配置。
如果没有这个包,MVC 将使用其默认控制器工厂,该工厂对注入依赖项一无所知。
| 归档时间: |
|
| 查看次数: |
2769 次 |
| 最近记录: |