How to inject dependencies using Ninject In ASP.NET WebForm?

给你一囗甜甜゛ 提交于 2019-12-01 08:02:51
Win

Install Ninject.Web either from "Package Manager Console" or NuGet.

Version is 3.2.1 as of this writing.

OR

It will install the following 4 packages -

Sample Service Class

public interface IUserService
{
    List<string> GetUsers();
}

public class UserService : IUserService
{
    public List<string> GetUsers()
    {
        return new List<string> {"john", "eric"};
    }
}

Then add binding to ~/App_Start/NinjectWebCommon.cs.

In code behind page, property inject using [Inject] attribute.

In Addition in answer by win I would advise people not to get confused by using Constructor based injection in ASP.NET Webforms as Web Forms doesn't support constructor based injection simply. In default configuration they only support Property based Injections as already demonstrated by Win.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!