是否可以在WebMethod
(http://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.90%29.aspx)? )中使用会话值
我尝试过使用System.Web.Services.WebMethod(EnableSession = true)
,但无法像in this example那样访问会话参数
[System.Web.Services.WebMethod(EnableSession = true)]
[System.Web.Script.Services.ScriptMethod()]
public static String checaItem(String id)
{
return "zeta";
}
下面是调用webmethod的JS:
$.ajax({
type: "POST",
url: 'Catalogo.aspx/checaItem',
data: "{ id : 'teste' }",
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(data);
}
});
发布于 2011-01-21 22:50:59
您可以使用:
HttpContext.Current.Session
但是,除非还指定了EnableSession=true
,否则它将为null
[System.Web.Services.WebMethod(EnableSession = true)]
public static String checaItem(String id)
{
return "zeta";
}
发布于 2013-10-21 19:04:51
有两种方法可以为Web方法启用会话:
1. [WebMethod(enableSession:true)]
2. [WebMethod(EnableSession = true)]
第一个带有构造函数参数enableSession:true
的函数对我不起作用。具有EnableSession
属性的第二个可以工作。
发布于 2016-11-10 17:50:46
对于启用会话,我们必须使用WebMethod(enableSession:true)
[WebMethod(EnableSession=true)]
public string saveName(string name)
{
List<string> li;
if (Session["Name"] == null)
{
Session["Name"] = name;
return "Data saved successfully.";
}
else
{
Session["Name"] = Session["Name"] + "," + name;
return "Data saved successfully.";
}
}
现在,要使用session检索这些名称,我们可以这样做
[WebMethod(EnableSession = true)]
public List<string> Display()
{
List<string> li1 = new List<string>();
if (Session["Name"] == null)
{
li1.Add("No record to display");
return li1;
}
else
{
string[] names = Session["Name"].ToString().Split(',');
foreach(string s in names)
{
li1.Add(s);
}
return li1;
}
}
因此,它将检索会话中的所有名称并显示。
https://stackoverflow.com/questions/4758575
复制相似问题