Unable to get session in webmethod in asp.net

I just found that the problem is not related to the web method.

This is caused by another problem.

I installed Session["PhotoId"] in regular aspx. But I can not get the value in the webMethod aspx page.

 [WebMethod(EnableSession=true)] public static string Submit(string data1, ...) { string test = HttpContext.Current.Session["PhotoId"]; // test is null } 

What should I do?

+7
source share
2 answers

As I see it, everything should be fine.

Since HttpContext.Current.Session is not null, session state is maintained here. Make sure you set Session ["PhotoId"].

You can check if this is the same session by exploring

  HttpContext.Current.Session.SessionID 

in both regular ASPX and WebMethod.

+13
source

You must use the ToString() method to use the session as a string.

 [WebMethod(EnableSession=true)] public static string Submit(string data1, ...) { string test = HttpContext.Current.Session["PhotoId"].ToString(); } 
0
source

All Articles