在ashx页面中获取session

在一般事务处理页面,要得到 Session的值会报 “未将对象引用设置到对象的实例”的异常

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

1、引入 命名空间:

using System.Web.SessionState;  



2、实现IRequiresSessionState接口,具体如下  

 
  /// <summary>  
  /// $codebehindclassname$ 的摘要说明  
  /// </summary>  
  [WebService(Namespace = "http://tempuri.org/")]  
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  public class AddUserInfo : IHttpHandler,IRequiresSessionState //就是这样显示的实现一下,不用实现什么方法  
  {  
  
      public void ProcessRequest(HttpContext context)  
      {  
  
    //这样你就可以如下 操作了  
  
                if(context.Session["userAccount"] != null)  
  
   {  
  
     string account = context.Session["userAccount"].ToString();  
  
   }  
  
    //...继续下面的代码  
  
  }  
  
}  
原文地址:https://www.cnblogs.com/101rico/p/2913313.html