Cannot get session in webmethod in asp.net

匿名 (未验证) 提交于 2019-12-03 08:42:37

问题:

I just find that the problem is not related to the webmethod.

It is caused by another problem.

I set the Session["PhotoId"] in a normal aspx. But I can't retrieve value in the webMethod of the aspx page.

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

What should I do?

回答1:

As I see, everything should be fine here.

As far as HttpContext.Current.Session is not null, session state is supported here. Please ensure, that you set Session["PhotoId"].

You can test whether it is the same session by examining

 HttpContext.Current.Session.SessionID 

in both normal ASPX and WebMethod.



回答2:

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

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!