如何解决生产中的“序列不包含元素”?

问题描述

我有一个 mvc 项目,它在 localhost 上运行得非常好,但在生产(plesk 托管)上它抛出 Sequence contains no element 错误:

  public ActionResult Index(string page = "")
        {          
            if (page == "")
                page = "Home";
            
            PageViewModel model;
            PageDTO dto;
            using (Db db = new Db())
            {
                if (!db.Pages.Any(x => x.Slug.Equals(page)))
                    return RedirectToAction("Index",new { page = ""});
                dto = db.Pages.Where(x => x.Slug == page).FirstOrDefault();
            }
            ViewBag.PageTitle = dto.Title;
            if (dto.HasSidebar == true) 
            {
                ViewBag.Sidebar = "Yes";
            }
            else
            {
                ViewBag.Sidebar = "No";
            }
            model = new PageViewModel(dto);
            return View(model);
        }

当我在本地主机上运行项目时它工作正常,但在生产(plesk 托管)上它给出错误:

enter image description here

此外,在我第一次访问域时部署后,它要求我提供用户凭据,但我的应用程序没有这种登录。

enter image description here

Global.asax 下的 Application_AuthenticateRequest 代码

 protected void Application_AuthenticateRequest()
        {
            if(User ==null) { return; }
            string username = Context.User.Identity.Name;

            string[] roles = null;

            using(Db db = new Db())
            {
                AccountDTO dto = db.Users.FirstOrDefault(x => x.EmailAdress == username);
                roles = db.UserRoles.Where(x => x.UserId == dto.Id).Select(x => x.Role.Name).ToArray();
            }
            IIdentity userIdentity = new GenericIdentity(username);
            IPrincipal newUserObj = new GenericPrincipal(userIdentity,roles);

            Context.User = newUserObj;
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...