ashx中使用session的方法

來源:互聯網
上載者:User
ashx中擷取session值的方法,大家參考使用吧WEB開發,在一般處理常式中,很容易得到 Request和Response對象,如:代碼如下:HttpRequest _request = context.Request;  HttpResponse _response = context.Response;   但是要得到 Session的值就沒有那麼簡單了。 比如如果要在ashx得到儲存在Session中的登入使用者資訊 Session["LoginUser"] 如果僅僅使用 context.Session["LoginUser"] 的話,是會報 “未將對象引用設定到對象的執行個體”的異常! 具體要使用下列方法:  代碼如下:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.SessionState; namespace DtlCalendar.Mobile.Site.Manage{    /// <summary>    /// DelApk 的摘要說明    /// </summary>    public class DelApk : IHttpHandler, IReadOnlySessionState    {        // IReadOnlySessionState :唯讀訪問Session        // IRequiresSessionState :讀寫訪問Session        public void ProcessRequest(HttpContext context)        {            string strID = context.Request["id"];            context.Response.Clear();            context.Response.ContentType = "text/plain";            int id;            string user;            if (int.TryParse(strID, out id) && IsLoged(context, out user))            {                string reslt = DataProvider.MobileDataProvider.CreateInstance().DelMApk(id).ToString();                BLL.LogOprHelper.Instance.InsertMLog(user, BLL.LogOpr.Delete, "DelApk result:" + reslt);                context.Response.Write(reslt);            }            else            {                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)                {                    user = _User.Account;                    return true;                }            }            user = string.Empty;            return false;        }         public bool IsReusable        {            get            {                return true;            }        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.