Jeff and .NET

The .NET musings of Jeff Putz

Sponsors

News

My Sites

Archives

February 2010 - Posts

Using Ninject with ASP.NET MVC 2

My favorite dependency injector, Ninject, has ascended to v2! I like this framework because it's small and doesn't have a bunch of external dependencies. It does take a little extra work to get it rolling with ASP.NET MVC 2, however.

There is an extension project for MVC, and you can get that here. The core of this project, which is only a few classes, is a derivative of HttpApplication and the DefaultControllerFactory. The rub here is that the latter has a breaking change from the first version of MVC. You'll have to modify the GetControllerInstance() method of NinjectControllerFactory, because the base class from the framework now requires a RequestContext object in that method (I'm pretty sure that's to facilitate areas). The new method looks like this:

protected override IController GetControllerInstance(RequestContext requestContext, 
Type controllerType)
{
    var controller = Kernel.TryGet(controllerType) as IController;

    if (controller == null)
        return base.GetControllerInstance(requestContext, controllerType);

    var standardController = controller as Controller;

    if (standardController != null)
        standardController.ActionInvoker = CreateActionInvoker();

    return controller;
}

Rebuild the project, you're ready to rock!

EDIT (3/2/10): Nate forked the code a bit, so the project now has an MVC2 version: http://github.com/enkari/ninject.web.mvc

Where to find the jQuery 1.4.1 vsdoc file

If you're like me and you were trying to update your ASP.NET MVC 2 RC2 project with the latest and greatest, and couldn't figure out where the new scripts are, they're here:

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\CSharp\Web\1033\EmptyMvcWebApplicationProjectTemplatev2.0.cs.zip\Scripts

That's where you'll find the jquery-1.4.1.min-vsdoc.js file (among others). I'm sure I wasn't the only one scratching my head.

More Posts