Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the Session Inside an ASP.Net ScriptMethod

I have a list of objects which I store in the session. This list then appears on a web page with little "X"s next to each item. When one of them is clicked I use Javascript to remove the item from the list on the page and then I send an AJAX call to the server to remove the item from the list in the session also. Here's where things get a little tricky. I'm using a ScriptMethod that looks like this (C#):

[System.Web.Services.WebMethod, System.Web.Script.Services.ScriptMethod]
    public static void removeListItem(string itemNumber)

The problem is that this is a static method which means I don't have access to the Page variable which in turn means I don't have access to the Session. Now the sessionID is sent with the request (which I also can't access) and the server has the session so I would assume that there's some way to take that ID and access the session. Is there a way to access the session from a static method like this? Thanks!

like image 312
Peter Avatar asked Jul 15 '26 08:07

Peter


1 Answers

Use HttpContext.Current.Session instead of direct call to Session

more info in this article

like image 78
Amr Elgarhy Avatar answered Jul 17 '26 20:07

Amr Elgarhy