2015-06-19 59 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