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); } });
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.
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.
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"; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With