一般处理程序在使用Session出现 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态

今天使用ajax在对ashx进行请求时使用Session["DataBase"]总是为null,会报错:只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态...

看了其他的博客,发现在一般处理程序中使用到Session,无论是不是调用其他的类中的方法使用到Session都需要添加引用和接口

ashx获取session值:

  1.首先添加引用:using System.Web.SessionState;

  2.一般处理程序类要继承IRequiresSessionState接口

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.SessionState;
 namespace Biobase_BigData.ashx
 {
     /// 
     /// Handler1 的摘要说明
     /// 
     public class ajaxHandler : IHttpHandler, IRequiresSessionState
{

//1.using System.Web.SessionState;
//2.IRequiresSessionState
context.Session["DataBase"]=...
}

 

 

你可能感兴趣的:(C#)