在 WebMethod 中设置 Session 对象(使用 EnableSession = true)不存储值
Set Session object inside a WebMethod (using EnableSession = true) not storing value
我正在通过 WebMethod 将变量传递给会话
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true)]
public static bool lookEventQ(int eventuid)
{
HttpContext.Current.Session["Q_EVENT_ID"] = eventuid;
return true;
}
使用jQuery调用:
$.ajax({
url: "<%= ResolveUrl("~/public-conference.aspx")%>/lookEventQ?eventuid=" + event,
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
//DO STUFF
}
});
但是,在另一个页面上,尝试访问这个会话变量:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (int.TryParse(Request.Form.Get(CONST_postKey), out eventID))
{
if(eventID == int.Parse(HttpContext.Current.Session["Q_EVENT_ID"])){
//Do other stuff
}
}
}
}
但是尝试访问的时候,Session是空的
找到解决方案。由于 asp 文件不包含将 属性 EnablePageMethods
设置为 true
的 ScriptManager,会话不会保持。
我正在通过 WebMethod 将变量传递给会话
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true)]
public static bool lookEventQ(int eventuid)
{
HttpContext.Current.Session["Q_EVENT_ID"] = eventuid;
return true;
}
使用jQuery调用:
$.ajax({
url: "<%= ResolveUrl("~/public-conference.aspx")%>/lookEventQ?eventuid=" + event,
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
//DO STUFF
}
});
但是,在另一个页面上,尝试访问这个会话变量:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (int.TryParse(Request.Form.Get(CONST_postKey), out eventID))
{
if(eventID == int.Parse(HttpContext.Current.Session["Q_EVENT_ID"])){
//Do other stuff
}
}
}
}
但是尝试访问的时候,Session是空的
找到解决方案。由于 asp 文件不包含将 属性 EnablePageMethods
设置为 true
的 ScriptManager,会话不会保持。