我有一个ASP.net MVC 4站点,它在第一次请求时变慢了。我在运行应用程序时尝试了断点。在登录过程中,它在我的第一个数据库查询中几乎停留了一分钟:
var InstnCode = form["code"].ToString();
var ComAccount = Context.Companies.Where(x => x.CompanyCode == InstnCode);
之后一切顺利。
为什么会这样,我该如何纠正这个过程。由于这个问题,有时我会得到一个Server Timeout error
.
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(UserProfile model, string returnUrl, FormCollection form)
{
var InstnCode = form["code"].ToString();
var ComAccount = Context.Companies.Where(x => x.CompanyCode == InstnCode);
if (ComAccount.Any())
{
var modelvalue =
(from d in Context.UserProfiles
where d.UserName == model.UserName && d.Password == model.Password && d.Company.CompanyCode == InstnCode
select d).FirstOrDefault();
if (modelvalue != null)
{
string code = null;
Session["UName"] = modelvalue.UserName;
Session["Theme"] = modelvalue.Theme;
Session["InstnName"] = modelvalue.Company.CompanyName;
Session["Role"] = modelvalue.Role.RoleName;
Session["StartUp"] = modelvalue.StartUp;
var permission =
Context.AccountPermissions.Where(x => x.RoleId == modelvalue.RoleId)
.AsQueryable()
.FirstOrDefault();
if (permission != null)
{
SetSessions(permission, "yes");
}
else
{
SetSessions(permission, "no");
}
if (modelvalue.CompanyId != 0 && modelvalue.StaffId == null && modelvalue.StudentProfileId == null)
{
Session["ComID"] = modelvalue.CompanyId;
code = modelvalue.Company.CompanyCode;
}
else if (modelvalue.CompanyId != 0 && modelvalue.StudentProfileId != null)
{
var student =
(from d in Context.StudentProfiles
where d.StudentProfileId == modelvalue.StudentProfileId
select d).FirstOrDefault();
code = student.Company.CompanyCode;
Session["ComID"] = student.CompanyId;
}
else if (modelvalue.CompanyId != 0 && modelvalue.StaffId != null)
{
var staff =
(from d in Context.Staff where d.StaffId == modelvalue.StaffId select d).FirstOrDefault();
code = staff.Company.CompanyCode;
Session["ComID"] = staff.CompanyId;
Session["StaffID"] = staff.StaffId;
}
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect");
return View(model);
}
}
ModelState.AddModelError("", "The institution code provided is incorrect");
return View(model);
}
这是我的登录功能。我正在使用
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { enctype = "multipart/form-data" }))
我的登录表单。
任何帮助将不胜感激。提前致谢