通过pagemethods访问时的Session问题
我在通过页面方法访问会话中的值时遇到问题。
下面的示例以简单的形式演示了该问题。页面上有三个按钮,每个按钮都会调用不同的页面方法,该方法又会访问我期望的会话中的相同变量。事实上,每个页面方法返回的值都是不同的,如下所示。
我只有在部署到网络服务器时才会遇到这个问题;当我在本地测试时这不是问题。
Example output:
Method Session Value
DoMethod1 597cbe1c-5391-4b93-af6b-0da0068b264b
DoMethod2 35df48aa-8a58-4008-b353-be7b9f950395
DoMethod3 35df48aa-8a58-4008-b353-be7b9f950395
DoMethod1 597cbe1c-5391-4b93-af6b-0da0068b264b
DoMethod2 597cbe1c-5391-4b93-af6b-0da0068b264b
DoMethod1 35df48aa-8a58-4008-b353-be7b9f950395
DoMethod3 35df48aa-8a58-4008-b353-be7b9f950395
DoMethod1 597cbe1c-5391-4b93-af6b-0da0068b264b
DoMethod2 597cbe1c-5391-4b93-af6b-0da0068b264b
DoMethod3 35df48aa-8a58-4008-b353-be7b9f950395
Imports System.Web.Services
Public Class MethodResult
Public MethodName As String
Public SessionID As String
Public SessionValue As Object
End Class
Partial Public Class sessiontest
Inherits System.Web.UI.Page
<WebMethod()> _
Public Shared Function DoMethod1() As MethodResult
If HttpContext.Current.Session("sharedvalue") Is Nothing Then
HttpContext.Current.Session("sharedvalue") = Guid.NewGuid
End If
Dim ret = New MethodResult
ret.MethodName = "DoMethod1"
ret.SessionID = HttpContext.Current.Session.SessionID
ret.SessionValue = HttpContext.Current.Session("sharedvalue")
Return ret
End Function
<WebMethod()> _
Public Shared Function DoMethod2() As MethodResult
If HttpContext.Current.Session("sharedvalue") Is Nothing Then
HttpContext.Current.Session("sharedvalue") = Guid.NewGuid
End If
Dim ret = New MethodResult
ret.MethodName = "DoMethod2"
ret.SessionID = HttpContext.Current.Session.SessionID
ret.SessionValue = HttpContext.Current.Session("sharedvalue")
Return ret
End Function
<WebMethod()> _
Public Shared Function DoMethod3() As MethodResult
If HttpContext.Current.Session("sharedvalue") Is Nothing Then
HttpContext.Current.Session("sharedvalue") = Guid.NewGuid
End If
Dim ret = New MethodResult
ret.MethodName = "DoMethod3"
ret.SessionID = HttpContext.Current.Session.SessionID
ret.SessionValue = HttpContext.Current.Session("sharedvalue")
Return ret
End Function
End Class
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="sessiontest.aspx.vb" Inherits="project1.sessiontest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
<script type="text/javascript">
function method1() {
PageMethods.DoMethod1(methodComplete);
}
function method2() {
PageMethods.DoMethod2(methodComplete);
}
function method3() {
PageMethods.DoMethod3(methodComplete);
}
//complete
function methodComplete(result) {
document.getElementById('output').innerHTML += "<tr><td>" + result.MethodName + "</td><td>" + result.SessionID + "</td><td>" + result.SessionValue + "</td></tr>";
}
</script>
<div>
<input type="button" value="Method 1" onclick="method1();" id="btn1" />
<br />
<input type="button" value="Method 2" onclick="method2();" id="Button1" />
<br />
<input type="button" value="Method 3" onclick="method3();" id="Button2" />
<br />
<table id="output" style="width:100%;">
<tr>
<th>Method Name</th>
<th>Session ID</th>
<th>Session Value</th>
</tr>
</table>
</div>
</form>
</body>
</html>
I have a problem with accessing values in the session through pagemethods.
The example below demonstrates the problem in a simple form. There are three buttons on the page and each of these invokes a different pagemethod, that, in turn accesses what I would expect to be the same variable from the session. In fact the value returned is different for each pagemethod, as demonstrated below.
I only get this problem when I deploy to the webserver; it's not an issue when I test it locally.
Example output:
Method Session Value
DoMethod1 597cbe1c-5391-4b93-af6b-0da0068b264b
DoMethod2 35df48aa-8a58-4008-b353-be7b9f950395
DoMethod3 35df48aa-8a58-4008-b353-be7b9f950395
DoMethod1 597cbe1c-5391-4b93-af6b-0da0068b264b
DoMethod2 597cbe1c-5391-4b93-af6b-0da0068b264b
DoMethod1 35df48aa-8a58-4008-b353-be7b9f950395
DoMethod3 35df48aa-8a58-4008-b353-be7b9f950395
DoMethod1 597cbe1c-5391-4b93-af6b-0da0068b264b
DoMethod2 597cbe1c-5391-4b93-af6b-0da0068b264b
DoMethod3 35df48aa-8a58-4008-b353-be7b9f950395
Imports System.Web.Services
Public Class MethodResult
Public MethodName As String
Public SessionID As String
Public SessionValue As Object
End Class
Partial Public Class sessiontest
Inherits System.Web.UI.Page
<WebMethod()> _
Public Shared Function DoMethod1() As MethodResult
If HttpContext.Current.Session("sharedvalue") Is Nothing Then
HttpContext.Current.Session("sharedvalue") = Guid.NewGuid
End If
Dim ret = New MethodResult
ret.MethodName = "DoMethod1"
ret.SessionID = HttpContext.Current.Session.SessionID
ret.SessionValue = HttpContext.Current.Session("sharedvalue")
Return ret
End Function
<WebMethod()> _
Public Shared Function DoMethod2() As MethodResult
If HttpContext.Current.Session("sharedvalue") Is Nothing Then
HttpContext.Current.Session("sharedvalue") = Guid.NewGuid
End If
Dim ret = New MethodResult
ret.MethodName = "DoMethod2"
ret.SessionID = HttpContext.Current.Session.SessionID
ret.SessionValue = HttpContext.Current.Session("sharedvalue")
Return ret
End Function
<WebMethod()> _
Public Shared Function DoMethod3() As MethodResult
If HttpContext.Current.Session("sharedvalue") Is Nothing Then
HttpContext.Current.Session("sharedvalue") = Guid.NewGuid
End If
Dim ret = New MethodResult
ret.MethodName = "DoMethod3"
ret.SessionID = HttpContext.Current.Session.SessionID
ret.SessionValue = HttpContext.Current.Session("sharedvalue")
Return ret
End Function
End Class
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="sessiontest.aspx.vb" Inherits="project1.sessiontest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
<script type="text/javascript">
function method1() {
PageMethods.DoMethod1(methodComplete);
}
function method2() {
PageMethods.DoMethod2(methodComplete);
}
function method3() {
PageMethods.DoMethod3(methodComplete);
}
//complete
function methodComplete(result) {
document.getElementById('output').innerHTML += "<tr><td>" + result.MethodName + "</td><td>" + result.SessionID + "</td><td>" + result.SessionValue + "</td></tr>";
}
</script>
<div>
<input type="button" value="Method 1" onclick="method1();" id="btn1" />
<br />
<input type="button" value="Method 2" onclick="method2();" id="Button1" />
<br />
<input type="button" value="Method 3" onclick="method3();" id="Button2" />
<br />
<table id="output" style="width:100%;">
<tr>
<th>Method Name</th>
<th>Session ID</th>
<th>Session Value</th>
</tr>
</table>
</div>
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要为您的 Web 方法启用会话状态。
示例:
查看这篇 msdn 文章了解更多信息。
You need to enable session state for your web methods.
Example:
check out this msdn article for more information.