【发布时间】:2015-06-17 18:18:13
【问题描述】:
我有以下代码将会话变量存储在一个 webmethod 中并在另一个 webmethod 中检索它,但是当我尝试检索它时该值显示为 null。
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public bool SubmitList1(string businessname )
{
Session["Company_Name"] = businessname;
SqlConnection con = new SqlConnection();
.......
.........
.........
}
这将是我尝试检索会话变量的第二个网络方法
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public bool addresslisting()
{
string companyname = (string)Session["Company_Name"];// this particular value is displaying null
......
........
}
【问题讨论】:
-
嗯,有什么办法可以避免在这里使用 Session 吗? Web 服务(如 HTTP)应该是无状态的——在 Web 服务中使用会话已经够糟糕的了,更不用说在 2 个不同的 Web 方法之间共享会话了。我不知道这是否可行。 ASP.NET 可能会根据 web 方法对会话进行序列化,因此 cookie 标识符是不同的。您可以在 Web 方法 1 中访问 Session["Company_Name"] 吗?如果是这样,我的猜测可能是正确的。
-
感谢您的回复,我可以访问 string companyname = (string)Session["Company_Name"];
-
我可以在 webmethod1 中访问此代码。
-
我给的解决方案不适合你吗?
标签: c# asp.net web-services asmx