国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ashx中使用session的方法(獲取session值)

2020-01-17 23:37:14
字體:
來源:轉載
供稿:網友

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宝清县| 威海市| 浦城县| 阿瓦提县| 湘乡市| 弋阳县| 嘉善县| 南溪县| 潢川县| 子洲县| 固镇县| 新建县| 清水河县| 平顶山市| 宜君县| 织金县| 郸城县| 辰溪县| 定兴县| 水富县| 南通市| 高陵县| 佳木斯市| 云林县| 杨浦区| 榕江县| 凤山市| 巴林左旗| 宜兴市| 柳林县| 白城市| 克山县| 嘉善县| 凤城市| 调兵山市| 镇雄县| 温泉县| 沁阳市| 东平县| 上高县| 石阡县|