Klawztro Klawztro - 1 month ago 14
C# Question

How can I access session in a webmethod?

Can i use session values inside a

?

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

Answer

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