[英]ASP.NET WebApi Session vs Static Variables
In a ASP.NET WebApi hosted in IIS 7, does it have access to session? 在IIS 7中托管的ASP.NET WebApi中,它是否可以访问会话? It appears
Session
is null on the HttpContext.Current
. 在
HttpContext.Current
上看起来Session
是null。
What is the difference between these two for storing a global variable? 这两个存储全局变量有什么区别?
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>;
}
}
and 和
private static Dictionary<string, string> connectionStrings = new Dictionary<string, string>();
Should I use session or static variables to store connection strings that are dynamically generated (long story)? 我应该使用会话或静态变量来存储动态生成的连接字符串(长篇故事)吗?
Well, a session variable is meant to be per user. 嗯,会话变量是每个用户。 A static variable is a variable that will be shared between all users.
静态变量是将在所有用户之间共享的变量。 So, I have no idea why would store a connection string per user, but if you need to do that, you can't use one static variable.
所以,我不知道为什么会为每个用户存储一个连接字符串,但是如果你需要这样做,你就不能使用一个静态变量。 Now, you could use a static variable and make it a Dictionary, where the key is the user, and the value is whatever you want to store.
现在,您可以使用静态变量并将其设置为Dictionary,其中键是用户,值是您要存储的任何值。 That would certainly work.
那当然会奏效。
Having said all that, you can mimic sessions using cookies (which ultimately is what sessions make use of anyway (usually)): See: Accessing Session Using ASP.NET Web API 说了这么多,你可以使用cookie模拟会话(这最终是会话使用的(通常是)):请参阅: 使用ASP.NET Web API访问会话
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.