最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小采 时间:2020-11-27 22:40:15
文档

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

ashx中使用session的方法(获取session值):WEB开发,在一般处理程序中,很容易得到 Request和Response对象,如: 代码如下:HttpRequest _request = context.Request; HttpResponse _response = context.Response; 但是要得到 Session的值就没有那么简单了。 比如如果要在ash
推荐度:
导读ashx中使用session的方法(获取session值):WEB开发,在一般处理程序中,很容易得到 Request和Response对象,如: 代码如下:HttpRequest _request = context.Request; HttpResponse _response = context.Response; 但是要得到 Session的值就没有那么简单了。 比如如果要在ash


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

文档

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

ashx中使用session的方法(获取session值):WEB开发,在一般处理程序中,很容易得到 Request和Response对象,如: 代码如下:HttpRequest _request = context.Request; HttpResponse _response = context.Response; 但是要得到 Session的值就没有那么简单了。 比如如果要在ash
推荐度:
标签: session session中 ashx
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top