Cannot get session in webmethod in asp.net
Asked Answered
D

2

9

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?

Dona answered 17/3, 2011 at 14:30 Comment(1)
check if the sessionid is same.Bouncy
L
14

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.

Lip answered 17/3, 2011 at 14:58 Comment(2)
The session Id is different. This helps me solve my problem. Thanks.Dona
How or why SessionId could be different?Mim
A
1

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(); 
}
Alonzo answered 20/5, 2016 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.