如何让 Ninject 将依赖项注入到我的 MVC 控制器中? [英] How do I let Ninject inject dependencies into my MVC controllers?
本文介绍了如何让 Ninject 将依赖项注入到我的 MVC 控制器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 UserManager 和 UserStore 注入控制器?Ninject 绑定是:
How do you inject UserManager and UserStore into a controller? Ninject bindings are:
kernel.Bind<ApplicationDbContext>().ToSelf().InRequestScope();
kernel.Bind(typeof(UserManager<>)).ToSelf().InRequestScope();
kernel.Bind(typeof(UserStore<>)).ToSelf().InRequestScope();
推荐答案
为了能够自动连接 MVC 控制器,您需要替换 MVC 的默认 IControllerFactory
或默认的 IDependencyResolver代码>.有一个 Ninject.MVC3 Nuget 包 可以为您实现依赖项解析器.您可以在 此 Ninject 集成页面.
To be able to auto-wire MVC controllers, you need to either replace MVC's default IControllerFactory
or the default IDependencyResolver
. There's a Ninject.MVC3 Nuget package that implements a dependency resolver for you. You can more information about how to integrate Ninject with MVC on this Ninject integration page.
这篇关于如何让 Ninject 将依赖项注入到我的 MVC 控制器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文