i am using:-
i keep getting the 'No parameterless constructor defined for this object.' for my AccountController. The AccountController is injected with Services. The bindings for these services are defined in the ServiceModule.
Find below the code for my MvcApplication in Global.asax.cs.
public class MvcApplication : NinjectHttpApplication // System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "Login", id = "" } // Parameter defaults
);
}
protected override void OnApplicationStarted()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
RegisterAllControllersIn(Assembly.GetExecutingAssembly());
}
protected override IKernel CreateKernel()
{
return new StandardKernel(new INinjectModule[] {
new ServiceModule(),
});
}
}
Rebuilding the Ninject.Web.Mvc against the ASP.Net MVC 2 dlls fixed the issue. The problem is with the NinjectControllerFactory class. The signature of the method to get the controller instance has changed in MVC 2.
IController GetControllerInstance(Type controllerType)
To
IController GetControllerInstance(
RequestContext requestContext, Type controllerType)
Make the necessary changes and rebuild the Ninject MVC extension and all works fine. Thanks to @Charlino for the suggestion.
For what ever reason, I have found that if your global.asax.cs inherits from NinjectHttpApplication OnApplicationStarted() does not get called. Change your OnApplicationStarted() to override Init(), and it should work.
See below:
public class MvcApplication : NinjectHttpApplication // System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "Login", id = "" } // Parameter defaults
);
}
public override void Init()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
RegisterAllControllersIn(Assembly.GetExecutingAssembly());
}
protected override IKernel CreateKernel()
{
return new StandardKernel(new INinjectModule[] {
new ServiceModule(),
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With