asp.net-mvc - 网站加载登录表单缓慢

标签 asp.net-mvc entity-framework

我有一个正在开发的 ASP.NET MVC 网站,我最近将它部署在测试服务器上,以便客户端可以提供一些初步反馈。

我注意到在实时服务器上加载登录表单有相当长的延迟,大约 20-30 秒。登录后,系统运行正常,并且响应迅速。

如果您注销并返回登录页面,则再次变慢。

apppool 缓存似乎不是问题,因为它每次都在页面上发生,而不仅仅是一次,而且所有项目似乎都在加载。

关于如何调试这个的任何建议?

下面是所有 Controller 继承的 BaseController,加上帐户登录 Controller 。

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();
    }

和帐户 Controller :
    [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);
    }

最佳答案

有几件事可以提高性能:

  • 首先也是最重要的:在 中部署您的站点Release 如果您对性能有任何关心的话。这里很棒article来自戴夫·沃德 (Dave Ward)。
    <compilation targetFramework="your framework version" debug="false">
  • 如果您没有使用 webforms查看引擎(我认为您不是)只需禁用它并使用 Razor只是为了更进一步,只允许 chtml 文件
    ViewEngines.Engines.Clear(); IViewEngine RazorEngine = new RazorViewEngine() { FileExtensions = new string[] { "cshtml" } }; ViewEngines.Engines.Add(RazorEngine);
  • 为应用程序池 (IIS 7) 配置空闲超时设置 Here's the link


  • 编辑1:

    根据您上次提到该应用程序在您本地运行良好的评论 IIS .我建议您开始专注于分析远程 IIS 上的请求。 ,这是一个 link到您可以使用的工具。

    为了跟踪成功的请求(并且您应该在您的情况下这样做)设置 status 200 这是一个 tutorial也是如此。

    关于asp.net-mvc - 网站加载登录表单缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29378095/

    相关文章:

    c# - 异步运行同步代码

    javascript - 将 ASP.NET MVC 应用程序部署到多个虚拟目录(URL 引用和 JS 文件问题)

    c# - MVC 3 Ajax.ActionLink 了解一些东西

    c# - 如何在 EF Core 2.1 中定义外键关系

    asp.net-mvc - 系统不支持异常: Specified method is not supported

    entity-framework - 如何通过 EF Model First 数据库架构升级避免数据丢失?

    asp.net-mvc - ASP.NET MVC 脚本包未呈现

    c# - 捆绑链接的 JavaScript 文件

    c# - 无法使用实例引用访问成员 'string.Format(string, params object[])';用类型名称限定它

    c# - 模拟 DbContext.set<T>.Add() EF6