Tôi có một phương pháp trên trang được đánh dấu là [WebMethod]
sử dụng một số trạng thái phiên làm một phần của hoạt động của nó. Sau khi tôi viết mã này, tôi đột nhiên có một bộ nhớ flash mà bạn cần sử dụng EnableSessionState
khi bạn sử dụng trạng thái phiên trong một [WebMethod]
(ví dụ: xem tại đây: http://msdn.microsoft.com/en-us/library/byxd99hx.aspx). Nhưng nó có vẻ hoạt động tốt. Tại sao?Tại sao có thể truy cập WebMethod Session State mà không có EnableSessionState?
mẫu sau:
protected void Page_Load(object sender, EventArgs args) {
this.Session["variable"] = "hey there";
}
[System.Web.Services.WebMethod]
public static string GetSessionVariable() {
return (string)HttpContext.Current.Session["variable"];
}
Mẫu nội dung html:
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function getSession() {
$.ajax({
type: 'POST',
url: 'Default.aspx/GetSessionVariable',
data: '{ }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
document.getElementById("showSessionVariable").innerHTML = msg.d;
}
});
return false;
}
</script>
<form id="form1" runat="server">
<div id="showSessionVariable"></div>
<button onclick='return getSession()'>Get Session Variable</button>
</form>