ASP.Net MVC And Ninject

04.10.09

Problem: You want to be able use the Ninject frame work with the new and shiny ASP.Net MVC framework.

Solution: Assembly: Ninject.Framework.MVC Contains an Abstract Base class you can Inherit from to do most of your heavy lifting.

Some Code

Global.asax.cs

public class MvcApplication : NinjectHttpApplication
{
      private static IKernel _kernal;
      protected override void RegisterRoutes(RouteCollection routes)
      {                 
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
          routes.MapRoute("Default", // Route name
                          "{controller}/{action}/{id}", // URL with parameters
                          new { controller = "Home", action = "Index", id = "" } // Parameter defaults
                         );
      }                 
     protected override IKernel CreateKernel()
     {
         if (_kernal == null)
         {
             var modules = new IModule[]
             {
                 new AutoControllerModule(Assembly.GetExecutingAssembly()),
                 new DemoModual()
             };                
             _kernal = new StandardKernel(modules);
         }              
         return _kernal;         
      }
}