How do i use Ninject 2 in an ASP.NET MVC 3 applica

2019-04-16 03:19发布

问题:

Ive searched everywhere, i can't seem to find a way to implement ninject in my project. Ive heard of deriving MvcApplication to the NinjectHttpApplication. But NinjectHttpApplication isnt found even if i add the lib to the reference. I can't find Ninject.Web.Mvc. Does anyone have a guide somewhere in order to make this work, all i want to do is be able to bind my interface from my domain to existing implementation.

回答1:

http://weblogs.asp.net/shijuvarghese/archive/2010/04/30/dependency-injection-in-nerddinner-app-using-ninject.aspx

Looks like the bulk of it is done via

Global.asax.cs
public class MvcApplication : NinjectHttpApplication

Replace Application_Start with

protected override void OnApplicationStarted()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new MobileCapableWebFormViewEngine());
    RegisterAllControllersIn(Assembly.GetExecutingAssembly());
}

Then create your Ninject modules and configure the kernel, all is explained in the blog post and source code is available here: http://nerddinneraddons.codeplex.com/

While the above is for MVC 2, it should still apply



回答2:

See my blog post about the MVC3 extension here: http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/

The source code at github comes with a full featured sample application: https://github.com/ninject/ninject.web.mvc

The binaries can be found either at github: https://github.com/ninject/ninject.web.mvc/archives/master Or on the build server: http://teamcity.codebetter.com/project.html?projectId=project3&tab=projectOverview