如何在 webmethod 中访问会话? [英] How can I access session in a webmethod?

查看:27
本文介绍了如何在 webmethod 中访问会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 WebMethod?

我尝试使用 System.Web.Services.WebMethod(EnableSession = true) 但我无法访问像 在本例中:

I've tried using System.Web.Services.WebMethod(EnableSession = true) but i can't access Session parameter like in this example:

    [System.Web.Services.WebMethod(EnableSession = true)]
    [System.Web.Script.Services.ScriptMethod()]
    public static String checaItem(String id)
    { 
        return "zeta";
    }

这是调用 webmethod 的 JS:

here's the JS who calls the webmethod:

    $.ajax({
        type: "POST",
        url: 'Catalogo.aspx/checaItem',
        data: "{ id : 'teste' }",
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            alert(data);
        }
    });

推荐答案

您可以使用:

HttpContext.Current.Session

但它将是 null 除非你还指定了 EnableSession=true:

But it will be null unless you also specify EnableSession=true:

[System.Web.Services.WebMethod(EnableSession = true)]
public static String checaItem(String id)
{ 
    return "zeta";
}

这篇关于如何在 webmethod 中访问会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆