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

在一般事务处理页面,可以轻松的得到 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变量名称"]

相关文章:

  • 2022-12-23
  • 2021-09-17
  • 2021-11-03
  • 2021-06-03
  • 2021-07-12
  • 2021-11-26
  • 2022-12-23
  • 2022-01-06
猜你喜欢
  • 2021-12-14
  • 2021-09-07
  • 2022-02-23
  • 2021-10-26
  • 2021-10-10
  • 2022-12-23
相关资源
相似解决方案