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

首頁(yè) > 編程 > .NET > 正文

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

2024-07-10 13:19:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

WEB開(kāi)發(fā),在一般處理程序中,很容易得到 Request和Response對(duì)象,如:

復(fù)制代碼 代碼如下:


HttpRequest _request = context.Request;

HttpResponse _response = context.Response;

但是要得到 Session的值就沒(méi)有那么簡(jiǎn)單了。

比如如果要在ashx得到保存在Session中的登錄用戶信息 Session["LoginUser"]

如果僅僅使用 context.Session["LoginUser"] 的話,是會(huì)報(bào) “未將對(duì)象引用設(shè)置到對(duì)象的實(shí)例”的異常!

具體要使用下列方法:

復(fù)制代碼 代碼如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace DtlCalendar.Mobile.Site.Manage
{
    /// <summary>
    /// DelApk 的摘要說(shuō)明
    /// </summary>
    public class DelApk : IHttpHandler, IReadOnlySessionState
    {
        // IReadOnlySessionState :只讀訪問(wèn)Session
        // IRequiresSessionState :讀寫(xiě)訪問(wèn)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;
            }
        }
    }
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 葫芦岛市| 郸城县| 同仁县| 西平县| 天津市| 拉萨市| 革吉县| 南康市| 宽甸| 罗平县| 临颍县| 泾阳县| 海南省| 谷城县| 珲春市| 毕节市| 澎湖县| 沭阳县| 双辽市| 云林县| 鹤壁市| 潜山县| 屯门区| 巴林右旗| 保亭| 凤城市| 温宿县| 襄城县| 云南省| 永济市| 上林县| 商洛市| 库伦旗| 安平县| 琼中| 仲巴县| 湛江市| 乌拉特前旗| 屯门区| 依兰县| 莫力|