I am testing Ninject, but following the instructions, I find it impossible to get it to work. Information on the Internet is so dirty that it is even contradictory. I am developing a website in MVC 4 at visual studio 2012 and I installed Ninject using Nuget.
So, I get an error: "A constructor without parameters is not defined for this object." As soon as I enter the controller.
I took the necessary steps:
- Install Nuget
- in NinjectWebCommon.cs, I registered my interface in the RegisterServices method.
In my home controller, I set my object as follows:
public ISurvey _survey { get; set; } [Inject] public HomeController(ISurvey s) { _survey = s; }
And I get the following error message:
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 So I wonder what I did wrong? 4 hours, I discovered that there may be a problem with my web.config file or that it may be a missing link, or it may be a factory missing. Right now I don’t even know what to do. Thank you for your help.
Dave Nov 21 '13 at 17:29 2013-11-21 17:29
source share