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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-19 我要评论
想了解ashx中使用session的方法(获取session值)的相关内容吗,在本文为您仔细讲解ashx获取session值的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:ashx中使用session,获取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;
            }
        }
    }
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们