How can I get ninject 2.0 while working with asp.net mvc 2?

I use: -

I keep getting "No parameterless constructor defined for this object." for my AccountController. Services are entered into the AccountController. Associations for these services are defined in the ServiceModule.

Find below 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(), }); } } 
+6
dependency-injection asp.net-mvc ninject asp.net-mvc-2
source share
4 answers

Restoring Ninject.Web.Mvc against ASP.Net MVC 2 DLL fixed the problem. The problem is the NinjectControllerFactory class. The signature of the method to get the controller instance has changed in MVC 2.

 IController GetControllerInstance(Type controllerType) 

For

 IController GetControllerInstance( RequestContext requestContext, Type controllerType) 

Make the necessary changes and rebuild the Ninject MVC extension, and everything works fine. Thanks @Charlino for the suggestion.

+7
source share

For some reason, I found that if your global.asax.cs file is inherited from NinjectHttpApplication OnApplicationStarted (), it will not be called. Modify 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(), }); } } 
+1
source share

In Ninject, you should not override the Application_Start method. If you need things that are not automatically executed (for example, regions of registration - I don’t know what, from my point of view, if Ninject does this for you), be sure to call base.OnApplicationStarted() to make all Ninject-specific stuff work fine .

 protected override void OnApplicationStarted() { base.OnApplicationStarted(); // Kick some butt here } 
-one
source share

The code looks correct, except for the comma after "new ServiceModule ()".

It should be in Global.asax.cs, not Global.aspx.cs.

-one
source share

All Articles