The Page set function of blog writing today searches for pages based on URLs. The LINQ statement obtains the qualified page from the GetModelList () set of SitePage. If the Page fails to be queried, it jumps to the 404 page. The Code is as follows:
Var page = (from p in optSitePage. getModelList () where p. virtualUrlPath. toLower () = requestTitle. toLower () select p ). first (); if (null = page) {context. response. redirect ("/404.html"); return ;}
I intentionally passed a nonexistent URL. The result of the linq statement directly reports an error: System. InvalidOperationException: Sequence contains no elements. I checked MSDN and found that if First () is used, if there is no element in the result set, it will not return null. Change to FirstOrDefault.
? Var page = (from p in optSitePage. GetModelList () where p. VirtualUrlPath. ToLower () = requestTitle. ToLower () select p). FirstOrDefault ();
Traceback http://www.wyjexplorer.cn/Home/View/D44F795B009E20FD.html
From Wang Yujie