【问题标题】:Set Session object inside a WebMethod (using EnableSession = true) not storing value在 WebMethod 中设置 Session 对象(使用 EnableSession = true)不存储值
【发布时间】:2016-08-06 10:10:09
【问题描述】:

我通过 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
            }
        }
    }
}

但是当tring访问时,Session是空的

【问题讨论】:

  • 你的webmethod调用成功了吗?
  • 是的。方法总是被前端调用

标签: jquery asp.net session webforms


【解决方案1】:

找到了解决办法。由于 asp 文件不包含属性 EnablePageMethods 设置为 true 的 ScriptManager,因此会话不会保持。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-05
    • 2015-06-28
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多