Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access session in a webmethod?

Tags:

c#

session

Can i use session values inside a WebMethod?

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

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);         }     }); 
like image 772
Sérgio Avatar asked Jan 21 '11 12:01

Sérgio


People also ask

How do I access my WebMethod session?

To enable the session in a web service it must use the EnableSession Property of the WebMethod attribute. It must set EnableSession to true as shown in the preceding example. Without setting the EnableSession property to true, the session will not work otherwise the unhandled exception occurs.

What is the use of WebMethod in asp net?

The WebMethod attribute is added to each method we want to expose as a Web Service. ASP.NET makes it possible to map traditional methods to Web Service operations through the System. Web. Services.


1 Answers

You can use:

HttpContext.Current.Session 

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"; } 
like image 108
WraithNath Avatar answered Oct 02 '22 08:10

WraithNath