Friday, February 21, 2014

ASP.NET MVC: Injecting dependencies into views

In ASP.NET MVC you can even apply dependency injection into views. You can use the IoC framework of your choice and injecting Dependencies into your views will just work.

Most of the time I create a base class to inject some common dependencies:

To use this page instead of the default ViewPage, you have to register this class inside the web.config in the Views folder: Then you can just start using these dependencies inside your Razor views:

Remark: The only thing you need to be aware of is that the Dependency injection does not work inside your layout view.

No comments: