The method of using session in ASHX
Last Update:2017-02-28
Source: Internet
Author: User
Ashx to get the session value of the method, we refer to the use of web development, in general processing programs, it is easy to get request and response objects, such as: The Code is as follows: HttpRequest _request = context. request; HttpResponse _response = context. Response; But it's not that easy to get the value of the session. For example, if you want to get the login user information saved in session ashx session["Loginuser" if only use context. session["Loginuser"] is the exception that will report "an object reference is not set to an instance of an object"! Use the following methods: Code as follows: using System; Using System.Collections.Generic; Using System.Linq; Using System.Web; Using System.Web.SessionState; namespace DtlCalendar.Mobile.Site.Manage { ///<summary> ///delapk Summary description ///</summary> public class Delapk:ihttphandler, ireadonlysessionstate { //ireadonlysessionstate: Read-only access session //irequiressessionstate: Read-Write access Sess Ion public void ProcessRequest (HttpContext context) { &NBSP ;   String Strid = context. request["id"]; context. Response.Clear (); context. Response.ContentType = "Text/plain"; int id; string user; if (int. TryParse (Strid, out IDs) && isloged (context, out user) { & nbsp String reslt = DataProvider.MobileDataProvider.CreateInstance (). delmapk (ID). ToString (); BLL. LogOprHelper.Instance.InsertMLog (user, BLL. Logopr.delete, "delapk result:" + reslt); context. Response.Write (RESLT); else &NB Sp { BLL. LogOprHelper.Instance.InsertMLog (Strid, BLL. Logopr.delete, "delapk result:-1"); context. Response.Write ("-1"); { private bool Isloged (HttpContext context, out string user) { BLL. User _user; if (context. session["Loginuser"]!= null) { _user = context. session["Loginuser"] as BLL. User; if (_user!= null) &NBSP ; { user = _user.account RETUrn true; } user = string. Empty; return false; } public bool isreusable {&nbs P get { &NBS P return true; { } }