MVC5和Ninject:无参数构造函数错误

 mobiledu2502873611 发布于 2023-01-31 18:38

在MVC 5中,我们得到了这样的帐户控制器.

public class AccountController : Controller
        {

            public AccountController()
                : this(new UserManager(new UserStore(new DatePickerDbContext())))
            {

            }

            public AccountController(UserManager userManager)
            {
                UserManager = userManager;
            }
        }

我安装了Ninject来处理我的依赖注入.我有两个存储库,我想在我的AccountController中使用,现在代码看起来像这样

    public class AccountController : Controller
    {
              private readonly ILicenserepository _licenserepository;
              private readonly IUserRepository _userRepository;    

        public AccountController(ILicenserepository licenserepository, IUserRepository userRepository)
            : this(new UserManager(new UserStore(new DatePickerDbContext())))
        {
            _licenserepository = licenserepository;
            _userRepository = userRepository;
        }

        public AccountController(UserManager userManager)
        {
            UserManager = userManager;
        }
    }

在ninject.web.common中,这就是我所做的

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind(typeof (ILicenserepository)).To(typeof (Licenserepository));
    kernel.Bind(typeof (IUserRepository)).To(typeof (UserRepository));
}    

但是当我运行应用程序时,我在浏览器上收到错误,说没有找到参数化的构造函数.如果我创建参数较少的构造函数,我的存储库不会被实例化.因此,无论我从存储库调用该方法,值都为null.我怎么解决这个问题?或者让MVC或Ninject知道要调用哪个构造函数?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有