The method of using session in ASHX

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 ;   &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;             {       }    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.