ashx文件调用Session变量的方法

请注意以下描红部分:
==========================

在一般事务处理页面,可以轻松的得到 Request,Response对象,从而进行相应的操作,如下:

HttpRequest Request = context.Request; 

HttpResponse Response = context.Response;

但是要得到 Session的值就没有那么简单了。比如你要在ashx得到保存在Session中的登录帐号Session["userAccount"]

如果你只是context.Session["userAccount"]的话是会报 “未将对象引用设置到对象的实例”的异常

所以,如果要想取Session中的值 ,需要如下所示

①导入命名空间

using System;
using System.Web;
using System.Web.SessionState; //第一步:导入此命名空间

②实现接口

public class MyWeb : IHttpHandler将此改为如下:

public class MyWeb : IHttpHandler ,IRequiresSessionState //第二步:实现接口 到此就可以像平时一样用Session了

③调用方法

1、HttpContext.Current.Session["Session变量名称"]

2、context.Session["Session变量名称"]

posted @ 2014-05-14 17:12  zagelover  阅读(160)  评论(0编辑  收藏  举报