How do I work with Ninject in an ASP.NET MVC Web App?

后端 未结 3 1540
别那么骄傲
别那么骄傲 2021-02-12 03:54

I\'ve created a new MVC Web application and I have references to Ninject.dll, Ninject.Web.Common.dll and Ninject.Web.MVC.dll.

Global.asax.cs:

         


        
相关标签:
3条回答
  • 2021-02-12 04:24

    To be explicitly clear about this, if you use NuGet to add the 'Ninject.Mvc3' package (I used version 3.0.0.6), there is no need to make any modifications to global.asax.cs. The NuGet package does the magic for you by creating the NinjectWebCommon class in the App_Start folder of your MVC 4 project.

    I say this because I seem to have followed a similar tutorial to the original poster (I followed an article on The Code Project called 'Dependency Injection in asp.net mvc4 and webapi using Ninject'), and had exactly the same issue as the original poster. The Code Project article doesn't make it clear that you should either use NuGet (and don't touch global.asax.cs or add the Ninject references manually (and amend global.asax.cs).

    0 讨论(0)
  • 2021-02-12 04:39

    You are deriving from NinjectHttpApplication AND you are using App_Start at the same time. Choose one! Read the docu of Ninject.MVC3 for more info.

    0 讨论(0)
  • 2021-02-12 04:47

    Be sure you are not referencing a project which also uses the NinjectMVC3 App_Start. After removing the reference to such, my project started working. Also, as said before, check the namespaces all match and are correct.

    0 讨论(0)
提交回复
热议问题