如何在 web 方法中访问会话?

Posted

技术标签:

【中文标题】如何在 web 方法中访问会话?【英文标题】:How can I access session in a webmethod? 【发布时间】:2011-06-13 02:45:40 【问题描述】:

我可以在WebMethod 中使用会话值吗?

我尝试过使用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);
        
    );

【问题讨论】:

发布代码示例将帮助我们为您提供答案。 你遇到异常了吗? 在上面的示例中,我没有看到您尝试访问任何会话值。您需要先设置会话变量,然后像您发布的链接一样访问它。 return (int) Session["Conversions"]; @volpav 他提供了示例代码。 不,@capdragon 页面的 Session 属性对于静态方法不存在(WebMethods 必须是静态的)——他问在哪里可以找到该属性——如下所示,它位于当前的 HttpContext。 【参考方案1】:

你可以使用:

HttpContext.Current.Session

但它将是null,除非您还指定EnableSession=true

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

【讨论】:

具有讽刺意味的是,这就是我已经在做的事情——只是它对我不起作用。 HttpContext.Current.Session.Count 返回 0(即 Session 中没有项目)。对我来说,答案就在问题中,将 [WebMethod] 更改为 [WebMethod(EnableSession = true)] 有效。哇! 记得配置web.config 【参考方案2】:

有两种方法可以为 Web 方法启用会话:

1. [WebMethod(enableSession:true)]

2. [WebMethod(EnableSession = true)]

第一个带有构造函数参数enableSession:true 对我不起作用。具有EnableSession 属性的第二个有效。

【讨论】:

我不能 figure out 如果第一个甚至编译 - 我相信它没有。第二个确实有效,因为您正在设置属性(在这里很明显 XD)。 @MVCDS 为什么你认为它不应该被编译?您可以在文档中找到公共构造函数WebMethodAttribute(Boolean) 你说得对。如果您不设置参数名称,它的行为会有所不同吗?因为如果是这样,当他们编写构造函数(用于属性)时会发生一些非常奇怪的事情。【参考方案3】:

对于启用会话,我们必须使用 [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.";
    



现在要使用会话检索这些名称,我们可以这样做

[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;
        

    

因此它将检索会话中的所有名称并显示。

【讨论】:

【参考方案4】:

你可以这样试试 [网络方法] public static void MyMethod(string ProductID, string Price, string Quantity, string Total)// 在此处添加新参数 db_class 连接字符串 = 新的 db_class(); 尝试

            DataTable dt = (DataTable)HttpContext.Current.Session["aaa"];

            if (dt == null)
            
                DataTable dtable = new DataTable();

                dtable.Clear();
                dtable.Columns.Add("ProductID");// Add new parameter Here
                dtable.Columns.Add("Price");
                dtable.Columns.Add("Quantity");
                dtable.Columns.Add("Total");
                object[] trow =  ProductID, Price, Quantity, Total ;// Add new parameter Here
                dtable.Rows.Add(trow);
                HttpContext.Current.Session["aaa"] = dtable;                   
            
            else
            
                object[] trow =  ProductID, Price, Quantity, Total ;// Add new parameter Here
                dt.Rows.Add(trow);
                HttpContext.Current.Session["aaa"] = dt;
            


        
        catch (Exception)
        
            throw;
        
    

【讨论】:

【参考方案5】:

如果启用了会话,请查看 web.config。这里的这篇文章可能会提供更多的想法。 https://***.com/a/15711748/314373

【讨论】:

【参考方案6】:

在 C# 中,使用 web 方法在页面后面的代码上,

[WebMethod(EnableSession = true)]
        public static int checkActiveSession()
        
            if (HttpContext.Current.Session["USERID"] == null)
            
                return 0;
            
            else
            
                return 1;
            
        

而且,在 aspx 页面中,

$.ajax(
                type: "post",
                url: "",  // url here
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: ' ',
                crossDomain: true,
                async: false,
                success: function (data) 
                    returnValue = data.d;
                    if (returnValue == 1) 

                    
                    else 
                        alert("Your session has expired");
                        window.location = "../Default.aspx";
                    
                ,
                error: function (request, status, error) 
                    returnValue = 0;
                
            );

【讨论】:

以上是关于如何在 web 方法中访问会话?的主要内容,如果未能解决你的问题,请参考以下文章

如何分析 Java/Seam Web 应用程序中的会话内存使用情况?

如何在 Java 中访问 HTTP 会话

如何在 Java 中访问 HTTP 会话

在 Grails 中,如何访问域类静态方法中的休眠会话?

如何在会话存储在状态服务器(Web Garden)中的多线程中使用会话变量

Laravel:如何访问 AppServiceProvider 中的会话值?