I have a method on a page marked as [WebMethod] that uses some session state as part of its operation. After I wrote this code, I suddenly had a flash of memory that you need to use EnableSessionState when you use the session state in [WebMethod] (for example, see here: http://msdn.microsoft.com/en- us / library / byxd99hx.aspx ). But it seems to be working fine. Why?
Code example:
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"]; }
Body html example:
<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>
user12861 Mar 22 '13 at 13:39 2013-03-22 13:39
source share