Ninject + MVC3 = InvalidOperationException异常:序列不包含任何元素 [英] Ninject + MVC3 = InvalidOperationException: Sequence contains no elements

查看:157
本文介绍了Ninject + MVC3 = InvalidOperationException异常:序列不包含任何元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的项目MVC3,按下F5键,看到了示例页面。

I created a new MVC3 project, hit F5, saw the sample page.

然后我用的NuGet来获取 Ninject.MVC扩展。我根据Ninject文件修改了我的Global.asax,如何设置一个MVC3应用程序

Then I used NuGet to get the Ninject.MVC extension. I modified my global.asax according to the Ninject documentation, How To Setup an MVC3 Application:

public class MvcApplication : NinjectHttpApplication
{
   public static void RegisterGlobalFilters(GlobalFilterCollection filters)
   {
       filters.Add(new HandleErrorAttribute());
   }

   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 });
   }

   protected override IKernel CreateKernel()
   {
       var kernel = new StandardKernel();
       kernel.Load(Assembly.GetExecutingAssembly());
       return kernel;
   }

   protected override void OnApplicationStarted()
   {
       base.OnApplicationStarted();

       AreaRegistration.RegisterAllAreas();
       RegisterGlobalFilters(GlobalFilters.Filters);
       RegisterRoutes(RouteTable.Routes);
   }
}

现在,当我运行应用程序,我得到的死亡的黄色画面,但以下情况除外:

Now when I run the app, I get the yellow screen of death with the following exception:

InvalidOperationException异常 - 序列不包含任何元素

InvalidOperationException - Sequence contains no elements.

在System.Linq.Enumerable.Single(...)

at System.Linq.Enumerable.Single(...)

在Ninject.Web.Mvc.Bootstrapper.Initialize(...)
  67行。

at Ninject.Web.Mvc.Bootstrapper.Initialize(...) line 67.

和足够的,<肯定href=\"https://github.com/ninject/ninject.web.mvc/blob/master/mvc3/src/Ninject.Web.Mvc/Bootstrapper.cs#L67\">line 67该文件调用。单(),从而抛出异常。

And sure enough, line 67 of that file calls .Single(), thus throwing the exception.

我在做什么错了?

推荐答案

您可以在安装后会注意到 ninject.mvc3 的NuGet有一个 App_Start 子文件夹中包含 NinjectMVC3.cs 文件项目内部创建的。删除此文件夹,然后再试一次。因此,这里是我遵循的步骤:

You might notice that after installing the ninject.mvc3 NuGet there is an App_Start subfolder created inside your project containing an NinjectMVC3.cs file. Delete this folder and try again. So here are the steps I followed:


  1. 使用默认模板创建一个新的ASP.NET MVC 3项目

  2. 调出程序包管理器控制台窗口(查看 - >其他窗口 - >程序包管理器控制台)

  3. 键入在命令行安装包ninject.mvc3

  4. 在你的问题替换的Global.asax 默认code与code

  5. 删除安装包的过程中创建的 AppStart的子文件夹

  6. 运行应用程序

  7. 欣赏 /首页/指数默认页面在谷歌Chrome网络浏览器中打开的美: - )

  1. Create a new ASP.NET MVC 3 project using the default template
  2. Bring up the Package Manager Console window (View -> Other Windows -> Package Manager Console)
  3. Type install-package ninject.mvc3 on the command line
  4. Replace the default code in Global.asax with the code in your question
  5. Delete the AppStart subfolder created during the installation of the package
  6. Run the application
  7. Enjoy the beauty of the /Home/Index default page opened in your Google Chrome web browser :-)

这篇关于Ninject + MVC3 = InvalidOperationException异常:序列不包含任何元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆