Ninject + MVC3 = InvalidOperationException异常:序列不包含任何元素 [英] Ninject + MVC3 = InvalidOperationException: Sequence contains no elements
问题描述
我创建了一个新的项目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:
- 使用默认模板创建一个新的ASP.NET MVC 3项目
- 调出程序包管理器控制台窗口(查看 - >其他窗口 - >程序包管理器控制台)
- 键入
在命令行安装包ninject.mvc3
- 在你的问题替换
的Global.asax
默认code与code - 删除安装包的过程中创建的
AppStart的
子文件夹 - 运行应用程序
- 欣赏
/首页/指数
默认页面在谷歌Chrome网络浏览器中打开的美: - )
- Create a new ASP.NET MVC 3 project using the default template
- Bring up the Package Manager Console window (View -> Other Windows -> Package Manager Console)
- Type
install-package ninject.mvc3
on the command line - Replace the default code in
Global.asax
with the code in your question - Delete the
AppStart
subfolder created during the installation of the package - Run the application
- Enjoy the beauty of the
/Home/Index
default page opened in your Google Chrome web browser :-)
这篇关于Ninject + MVC3 = InvalidOperationException异常:序列不包含任何元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!