为ashx文件启用session管理

来源:互联网 发布:淘客代理系统源码 编辑:程序博客网 时间:2024/06/05 18:11

ashx中默认不能使用session, HttpContext.Current.Session 为 null。

解决方法如下:

增加命名空间引用,实现指定接口

  1. <%@ WebHandler Language="C#" Class="Handler" %>  
  2.  
  3. using System;  
  4. using System.Web;  
  5. using System.Web.SessionState;  
  6.  
  7. publicclass Handler : IHttpHandler, IRequiresSessionState {  
  8.      
  9.     publicvoid ProcessRequest (HttpContext context) {  
  10.          context.Response.ContentType = "text/plain";  
  11.          context.Response.Write("Hello World");  
  12.      }  
  13.  
  14.     publicbool IsReusable {  
  15.         get {  
  16.             returnfalse;  
  17.          }  
  18.      }  
  19.  
原创粉丝点击