Using ninject dependecyResolver for both MVC and WebAPI

前端 未结 2 458
别跟我提以往
别跟我提以往 2020-12-31 16:19

I have created and MVC 4 web application and decided to use web api in this app. I\'m using ninject dependency resolver for MVC web app. and now I want to use this ninject d

2条回答
  •  灰色年华
    2020-12-31 16:46

    There is a NuGet package that does this. Add the NInject, NInject.Web.Common, NInject.MVCx and WebApiContrib.IoC.Ninject NuGet packages to your project. A NInjectWebCommon class should have been created in the App_Start folder. Add your binding for your dependencies to the RegisterServices method. In the CreateKernel method after the RegisterServices(kernel) call, add the following line:

    GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
    

    This will let you create the Ninject dependency resolver without having to create your own override class. Easy, right?

提交回复
热议问题