【发布时间】:2014-06-26 23:17:08
【问题描述】:
我正在使用 aspx 网络表单和 AJAX 调用的组合。我想在 C# 端私下存储使用 ajax 来回传递的变量(然后最终在注销之前将这些变量保存到 SQL 数据库中)。
这是我的一些 javascript 文件的样子:
$.ajax({
type: "POST",
url: "Index.aspx/DoStuff",
data: JSON.stringify({ 'someVariable': varStr }),
...
});
我Index.aspx.cs的主要结构是这样的:
static SomeCollection g_Collection;
protected void Page_Load(object sender, EventArgs e)
{
g_Collection = new SomeCollection();
}
[WebMethod]
public static string DoStuff(string someVariable)
{
g_Collection.Add(someVariable);
}
// At some point ajax also calls another method that saves
// the g_Collection to a database *for that user* (important)
// - based on their GUID
对于单个用户,一切正常,但是,当多个用户登录时,g_Collection 被保存为一个单独使用的变量(正如预期的那样,因为 g_Collection 变量是 static)。
即如果有 2 个登录用户调用 DoStuff(),他们将添加到同一个 g_Collection 实例中。
由于方法DoStuff() 是static,我也将g_Collection 声明为static。
如何改为创建 g_Collection 的实例?
我需要声明什么(在 Page_Load() 内?)以便 g_Collection 是登录用户的实例,同时仍保持相同的功能我的static[WebMethod] 方法?
【问题讨论】:
标签: c# javascript asp.net ajax