Asp.net mvc 网站加载登录表单速度慢
我正在开发一个ASP.NETMVC网站,最近我将其部署在一个测试服务器上,以便客户机能够提供一些初始反馈 我注意到,在live服务器上,加载登录表单有相当大的延迟,大约20-30秒。登录后,系统功能正常,响应迅速 如果您注销并返回到登录页面,则再次变慢 apppool后台运行似乎没有问题,因为它在页面上每次都会发生,而不是一次,而且所有项目似乎都在加载 关于如何调试这个有什么建议吗 下面是BaseController,所有控制器都从该BaseController升级,加上帐户登录控制器Asp.net mvc 网站加载登录表单速度慢,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我正在开发一个ASP.NETMVC网站,最近我将其部署在一个测试服务器上,以便客户机能够提供一些初始反馈 我注意到,在live服务器上,加载登录表单有相当大的延迟,大约20-30秒。登录后,系统功能正常,响应迅速 如果您注销并返回到登录页面,则再次变慢 apppool后台运行似乎没有问题,因为它在页面上每次都会发生,而不是一次,而且所有项目似乎都在加载 关于如何调试这个有什么建议吗 下面是BaseController,所有控制器都从该BaseController升级,加上帐户登录控制器 prot
protected override void ExecuteCore()
{
if (User.Identity.IsAuthenticated)
{
try
{
AccountDataContext = new AccountDAL.DataContext(ConfigurationManager.AppSettings["Server"]);
// set the current user.
CurrentUser = AccountDataContext.Users.FirstOrDefault(x => x.Email == User.Identity.Name);
AccountDataContext.CurrentAccount = CurrentUser.Account;
ViewBag.CurrentUser = CurrentUser;
ViewBag.Account = CurrentUser.Account;
SystemDataContext = new SystemDAL.DataContext(ConfigurationManager.AppSettings["Server"], CurrentUser.Account.Database);
// setup the account based on the users settings
ViewBag.Theme = "Default"; // hard coded for now
}
catch (Exception)
{
// if the previous threw an exception, then the logged in user has been deleted
// log them out
FormsAuthentication.SignOut();
Session.Abandon();
// clear the authentication cookie
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie);
FormsAuthentication.RedirectToLoginPage();
}
}
base.ExecuteCore();
}
及客户总监:
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if(AccountDataContext == null)
AccountDataContext = new AccountDAL.DataContext(ConfigurationManager.AppSettings["Server"]);
var user = AccountDataContext.Users.FirstOrDefault(x => x.Email == model.UserName && x.Password == model.Password);
if (user != null)
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
以下几点可以提高性能:
- 首先也是最重要的一点:如果您关心性能,请以发布模式部署您的站点。这是戴夫·沃德写的一篇关于它的好文章
- 如果您没有使用
查看引擎(我假设您没有),请禁用它,仅使用webforms
,并进一步使用它,只允许使用chtml文件Razor
ViewEngines.Engines.Clear(); IViewEngine RazorEngine=新RazorViewEngine(){FileExtensions=新字符串[]{“cshtml”}; ViewEngines.Engines.Add(RazorEngine)代码>
- 为应用程序池(IIS 7)配置空闲超时设置
编辑1: 根据您最近的评论,该应用程序在您的本地
IIS
中运行良好。我建议您开始专注于分析远程IIS
上的请求,这里有一个工具供您使用
为了跟踪成功的请求(在您的情况下,您应该这样做),请将
状态设置为200,这里还有一个相关的设置。在应用程序池设置中,有一个名为加载用户配置文件的设置。将其设置为true,它将帮助您加快站点速度。注释出您的数据访问代码并运行它。这会加快速度吗?或者添加Debug.WriteLine(System.DateTime.Now)并查看长间隔在哪里。连接到数据库时可能会有延迟因为减速似乎只会影响登录页面,我仔细查看了他们都使用的帐户控制器(此时没有实现其他帐户功能)
我在控制器顶部发现以下代码:
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if(AccountDataContext == null)
AccountDataContext = new AccountDAL.DataContext(ConfigurationManager.AppSettings["Server"]);
var user = AccountDataContext.Users.FirstOrDefault(x => x.Email == model.UserName && x.Password == model.Password);
if (user != null)
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
publiccountcontroller()
:此(新的UserManager(新的UserStore(新的ApplicationDbContext()))
删除此声明可以解决此问题。我不确定它是什么,也不确定它来自哪里,我认为它来自原始的MS默认实现身份验证。我没有ApplicationDbContext,因此我认为它正在等待此请求超时,然后再继续。不相关,但是,您的本地(或非)上下文是如何处理的?当没有用户经过身份验证时会发生什么情况?在executeCore中,CurrentUser应该为null,不是吗?所以AccountDataContext.CurrentAccount=CurrentUser.Account代码>应该引发异常,然后重定向到登录页面,然后引发,然后…是的数据上下文:)@tschmit007我不处理它们:我在哪里可以这样做?关于当前用户-检查“User.Identity.IsAuthenticated”将为false,因此如果没有登录的用户,则不会执行任何检查。还可能值得注意的是,当通过Vs/IIS Express运行时,系统在我的本地计算机上运行良好。站点以发布模式部署。我正在使用IIS7.5,但应用程序池设置为空闲时不关闭。我不认为这是问题所在,因为每次访问或刷新登录页面时都会发生这种情况。您使用的是Microsoft.AspNet.Identity的哪个版本?