2016-05-24 3 views
1

Я работаю над веб-сайтом. Я использую как MVC 5, так и Web api. TTFB слишком длинный (6 ~ 10 с) для запроса веб-api, но ttfb представления mvc (1 ~ 2 с) является приемлемым. И когда я отправляю много запросов на один и тот же api, проблема только в первом запросе.Как уменьшить время (TTFB) почтового запроса?

это мой код в методе Вход

public HttpResponseMessage Login(LoginInfo loginInfo) 
    { 
     string result = JsonConvert.SerializeObject(new { status = "example" }); 

     //check the whether user is already login 
     if (HttpContext.Current.User.Identity.IsAuthenticated 
      && loginInfo.UserName == (string)HttpContext.Current.Session["user"]) 
     { 
      result = JsonConvert.SerializeObject(new { status = (string)HttpContext.Current.Session["role"] }); 
      return new HttpResponseMessage { Content = new StringContent(result, Encoding.UTF8, "application/json") }; 
     } 

     Student student = studentRepo.GetById(loginInfo.UserName); 
     if (student == null) 
     { 
      result = JsonConvert.SerializeObject(new { status = "usr" }); 
     } 
     else 
     { 
      string password; 
      string salt = (string)HttpContext.Current.Session["salt"]; 
      if (string.IsNullOrEmpty(salt)) 
      { 
       //the login page has expired 
       result = JsonConvert.SerializeObject(new { status = "expire" }); 
      } 
      else 
      { 
       password = student.Password + salt; 
       password = MD5Helper.GetMd5(password); 
       if (password == loginInfo.Password) 
       { 
        //login success! 
        HttpContext.Current.Session.Remove("salt"); 
        FormsAuthentication.SetAuthCookie(loginInfo.UserName, false); 
        HttpContext.Current.Session.Add("user", student.StuNo); 
        HttpContext.Current.Session.Add("role", student.Role); 
        result = JsonConvert.SerializeObject(new { status = student.Role }); 
       } 
       else 
       { 
        result = JsonConvert.SerializeObject(new { status = "deny" }); 
       } 
      } 
     } 
     return new HttpResponseMessage { Content = new StringContent(result, Encoding.UTF8, "application/json") }; 
    } 

ответ

0

Теперь я разворачивал мое приложение на другой сервер и TTFB стал короче. И проблема с этой сессией была удалена часто также исправлена.

Смежные вопросы