2013-02-11 74 views
6

在IIS 7中託管的ASP.NET WebApi中,它是否有權訪問會話?在HttpContext.Current上看起來Session爲空。ASP.NET WebApi會話與靜態變量

這兩個存儲全局變量有什麼區別?

private static Dictionary<string, string> ConnectionStrings 
    { 
     get 
     { 
      if (HttpContext.Current.Session["ConnectionStrings"] == null) 
       HttpContext.Current.Session["ConnectionStrings"] = new Dictionary<string, string>(); 

      return HttpContext.Current.Session["ConnectionStrings"] as Dictionary<string, string>; 
     } 
    } 

private static Dictionary<string, string> connectionStrings = new Dictionary<string, string>(); 

我應該使用會話或靜態變量來存儲動態生成的(長的故事)連接字符串?

回答

6

那麼,一個會話變量的意思是用戶。靜態變量是將在所有用戶之間共享的變量。所以,我不知道爲什麼會存儲連接字符串用戶,但如果你需要這樣做,你不能使用一個靜態變量。現在,您可以使用一個靜態變量並將其設置爲Dictionary,其中鍵是用戶,並且該值是您想要存儲的任何值。那肯定會奏效。儘管如此,您可以使用cookie模擬會話(最終會話會使用什麼(通常)):請參閱:Accessing Session Using ASP.NET Web API