How can I access a session in a web method?

Can I use session values ​​inside WebMethod ?

I tried using System.Web.Services.WebMethod(EnableSession = true) , but I cannot access the session parameter, for example in this example :

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

here's the JS that calls the web method:

  $.ajax({ type: "POST", url: 'Catalogo.aspx/checaItem', data: "{ id : 'teste' }", contentType: 'application/json; charset=utf-8', success: function (data) { alert(data); } }); 
+65
c # session
Jan 21 '11 at 12:03
source share
5 answers

You can use:

 HttpContext.Current.Session 

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

 [System.Web.Services.WebMethod(EnableSession = true)] public static String checaItem(String id) { return "zeta"; } 
+89
Jan 21 '11 at 14:50
source share

There are two ways to enable a session for a web method:

 1. [WebMethod(enableSession:true)] 2. [WebMethod(EnableSession = true)] 

The first argument to the enableSession:true constructor does not work for me. The second one with the EnableSession property works.

+9
Oct 21 '13 at 11:04 on
source share

You can try this [WebMethod] public static void MyMethod (ProductID string, Price string, Quantity string, Total string) // Add a new parameter Here {db_class Connstring = new db_class (); try {

  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; } } 
0
May 23 '16 at 17:53 on
source share

Take a look at your web.config if the session is enabled. This post here can give more ideas. stack overflow

0
Sep 01 '16 at 11:41
source share

To enable a session, we must use [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."; } } 

Now, to get these names using a session, we can go like this:

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

so he will select all the names from the session and show.

0
Nov 10 '16 at 9:50
source share



All Articles