开发者

ASP.Net MVC 3: Dependency injection with Ninject in View

How to dependenc开发者_运维知识库y injection using Ninject in View in MVC 3? I am now using Ninject 2.2 and Ninject.MVC3 2.2.2.0


You shouldn't be using Ninject to supply anything to the View. Ninject supplies dependencies to the Controllers, it is the job of the controller to build and pass models required by the View.

There are plenty of good tutorials around, see here for example.


Take a look on previous answers, do you really need this? View injection is in contradiction with MVC pattern and should be avoided in most cases.

But if you need some service to render some parts of a view you can do following:

  1. DependencyResolver.Current.GetService<IViewService>(), but I don't recommend to do this way as this is well known "service locator" antipattern.
  2. Here I found a better approach
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜