加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – ASP.NET MVC 2,Ninject 2.2并没有为此对象定义的

发布时间:2020-12-16 03:25:51 所属栏目:asp.Net 来源:网络整理
导读:所以我花了一些时间与ASP.NET MVC 2(目前坚持使用Visual Studio 2008),现在已经开始使用Ninject 2.2及其MVC集成.我从以下位置下载了Ninject 2.2和Ninject.Web.Mvc: https://github.com/downloads/ninject/ninject/Ninject-2.2.0.0-release-net-3.5.zip http
所以我花了一些时间与ASP.NET MVC 2(目前坚持使用Visual Studio 2008),现在已经开始使用Ninject 2.2及其MVC集成.我从以下位置下载了Ninject 2.2和Ninject.Web.Mvc:

https://github.com/downloads/ninject/ninject/Ninject-2.2.0.0-release-net-3.5.zip
https://github.com/downloads/ninject/ninject.web.mvc/Ninject.Web.Mvc2-2.2.0.0-release-net-3.5.zip

并在我的MVC 2项目中引用它们.我的Global.asax.cs文件看起来像这样(几乎是Ninject.Web.Mvc自述文件所说的):

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject.Web.Mvc;
using Ninject;

namespace Mvc2 {
    public class MvcApplication : NinjectHttpApplication {
        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                    "Default",// Route name
                    "{controller}/{action}/{id}",// URL with parameters
                    new { controller = "Home",action = "Index",id = UrlParameter.Optional } // Parameter defaults
            );
        }

        protected override void OnApplicationStarted() {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }

        protected override IKernel CreateKernel() {
            var kernel = new StandardKernel();

            kernel.Bind<IFoo>().To<Foo>();

            return kernel;
        }
    }
}

和一个看起来像这样的家庭控制器:

using System;
using System.Web;
using System.Web.Mvc;

namespace Mvc2.Controllers {
    public class HomeController : Controller {
        private readonly IFoo foo;

        public HomeController(IFoo foo) {
            this.foo = foo;
        }

        public ActionResult Index() {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }
    }
}

现在,每当我运行我的项目并访问’/’时,我会看到一个黄色的死亡屏幕,上面写着“没有为此对象定义无参数构造函数”.似乎Ninject没有解析我的Foo服务并将其注入HomeController.我想我错过了一些非常明显的东西,但我只是没有看到它.

如何让Ninject将Foo注入HomeController,而不使用Ninject属性?

解决方法

我:您能否提供有关IFoo服务实施的更多信息?它是否满足所有自己的依赖项?

我自己:嗯,不,不.结果我没有绑定它的依赖项.男孩,是错误信息和堆栈跟踪误导!

所以我的错误是我没有绑定IFoo实现的一个依赖项,因此Ninject默默地失败并试图继续它的快乐方式.这真的很不幸,因为一旦我偏离了琐碎的设置,它可能会导致一些非常奇怪的行为.我想我的下一个问题应该是如何尽早让Ninject失败并提供有关错误的好消息?但是现在我至少可以继续下去了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读