2015-06-19 57 views
2

我正在开发一个Asp.net MVC Web应用程序,并且现在已经部署到Web服务器上几个月了。就在最近,我遇到了一个让我困惑的新错误。在发布我的应用程序,并加载了该网站,我收到以下错误信息:序列不包含服务器端的元素

Sequence contains no elements 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Sequence contains no elements 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[InvalidOperationException: Sequence contains no elements] 
System.Linq.Enumerable.First(IEnumerable`1 source) +514 
System.Linq.Queryable.First(IQueryable`1 source) +330 
LabSys.MvcApplication.Application_AuthorizeRequest(Object sender, EventArgs e) +1261 
    System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92 
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&completedSynchronously) +165 

我很困惑,因为应用程序运行完全在本地主机上,没有任何问题或错误消息。我也检查了我的数据库,它也连接并验证到服务器。所以应该没有错误。

感谢您提前提供任何帮助!

+1

凡在你的代码是这样抛出异常,你可以表现出一定的代码? – Oluwafemi

+0

这是我坚持的部分。通过本地主机查看网站时,不会引发错误。一旦我通过发布的服务器查看它,它就会在每一页上引发。 –

+0

此错误是否在应用程序的着陆页/主页上发生。如果我没有错,你是在那里查询数据库,你没有检查返回的集合是否有任何元素,你正在使用它。 –

回答

0

检查你的代码,以确保FirstOrDefault()的SingleOrDefault()被替代使用的任何一种:第()单()

当你theLINQ错误“序列不包含任何元素”,这是 通常是因为你使用的是第一个()或单次()命令,而不是 FirstOrDefault()和的SingleOrDefault()。

Sequence contains no elements?

+0

非常感谢!我完全查看了在每个请求中调用的Global.asax.cs文件中的一行代码,从而导致了一个空值。 –

+1

快乐哥们! – Oluwafemi