Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从静态WebMethod引用非静态对象?-aspx,ajax_C#_Javascript_Asp.net_Ajax - Fatal编程技术网

C# 如何从静态WebMethod引用非静态对象?-aspx,ajax

C# 如何从静态WebMethod引用非静态对象?-aspx,ajax,c#,javascript,asp.net,ajax,C#,Javascript,Asp.net,Ajax,我使用的是aspxwebforms和AJAX调用的组合。我想在C#end上私密地存储使用ajax来回传递的变量(然后在注销之前最终将这些变量保存到SQL数据库) 我知道了,但我还是不确定 下面是我的一些javascript文件的外观: $.ajax({ type: "POST", url: "Index.aspx/DoStuff", data: JSON.stringify({ 'someVariable': varStr }), ... }); 我的Index.aspx.cs

我使用的是aspxwebforms和AJAX调用的组合。我想在C#end上私密地存储使用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_集合
被保存为一个单独使用的变量(因为
g_集合
变量是
static
)。
即如果两个登录用户调用
DoStuff()
它们将添加到相同的
g_集合
实例中


由于方法
DoStuff()
static
的,我也将
g_Collection
声明为
static

如何创建
g_Collection
的实例?
我需要声明什么(在
Page_Load()
?)以使g_Collection成为登录用户的实例,同时在我的
static
[WebMethod]中保持相同的功能
方法?

我不确定您为什么要在此场景中使用静态成员

  • 如果您希望每个用户都有一个集合,那么您可能只需要使用一个对象
  • 如果您想要一个全局集合(所有用户为1),则需要一个对象

当我们在不同选项卡中打开应用程序时,会话方法失败。两个选项卡可能发送不同的数据。