Sam*_*Sam 7 asp.net asp.net-web-api
在IIS 7中托管的ASP.NET WebApi中,它是否可以访问会话?它似乎Session是空的HttpContext.Current.
这两个存储全局变量有什么区别?
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>;
}
}
Run Code Online (Sandbox Code Playgroud)
和
private static Dictionary<string, string> connectionStrings = new Dictionary<string, string>();
Run Code Online (Sandbox Code Playgroud)
我应该使用会话或静态变量来存储动态生成的连接字符串(长篇故事)吗?
嗯,会话变量是每个用户.静态变量是将在所有用户之间共享的变量.所以,我不知道为什么会为每个用户存储一个连接字符串,但如果你需要这样做,你就不能使用一个静态变量.现在,您可以使用静态变量并将其设置为Dictionary,其中键是用户,值是您要存储的任何值.那当然会奏效.
说了这么多,你可以使用cookie模拟会话(这最终是会话使用的(通常是)):请参阅:使用ASP.NET Web API访问会话
| 归档时间: |
|
| 查看次数: |
8615 次 |
| 最近记录: |