如何让ninject 2.0与asp.net mvc 2一起使用?

Jit*_*til 6 asp.net-mvc dependency-injection ninject asp.net-mvc-2

我在用:-

  1. asp.net mvc rc 2
  2. Ninject和ninject asp.net mvc扩展(http://github.com/enkari/ninject.web.mvc)

我一直在为这个对象定义'No parameterless constructor'.为我的AccountController.AccountController注入了Services.这些服务的绑定在ServiceModule中定义.

在Global.asax.cs中找到我的MvcApplication的代码.

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(),
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

Jit*_*til 7

针对ASP.Net MVC 2 dll重建Ninject.Web.Mvc修复了这个问题.问题出在NinjectControllerFactory类上.获取控制器实例的方法的签名在MVC 2中已更改.

IController GetControllerInstance(Type controllerType)
Run Code Online (Sandbox Code Playgroud)

IController GetControllerInstance(
        RequestContext requestContext, Type controllerType)
Run Code Online (Sandbox Code Playgroud)

进行必要的更改并重建Ninject MVC扩展,一切正常.感谢@Charlino提出的建议.