Retrieving a Session Inside ASP.Net ScriptMethod

I have a list of objects that I store in a session. This list is then displayed on the web page with a small β€œX” 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. Here, where things get a little more complicated. I am using ScriptMethod, which 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 that I do not have access to the page variable, which in turn means that I do not have access to the session. Now, the sessionID is sent with a request (which I also can’t access), and the server has a session, so I would suggest that there is a way to accept this identifier and access the session. Is there a way to access the session from a static method like this? Thank!

+3
source share
1 answer

Use HttpContext.Current.Sessioninstead of direct callSession

more information in this article

+8
source

Source: https://habr.com/ru/post/1736201/


All Articles