在asp.net handler 中 使用 session

首先 handler 默認情況下是不支持 session 的。如果想在一般處理程序(handler)中使用session ,需要實現一個不需要實現任何方法的接口IRequiresSessionState

引入 System.Web.SessionState 即可。此時需要context.Session["pars"]這樣引用。具體實例如下圖所示:


下面是網友的文章:

參考文章1

原文鏈接


抽象类

[csharp]  view plain copy print ?
  1. using System;  
  2. using System.Web;  
  3. using System.Web.SessionState;  
  4. ....  
  5.   
  6. namespace SRERC.Web.admin  
  7. {  
  8.     /// <summary>  
  9.     /// SessionAwareHandler 的摘要说明  
  10.     /// </summary>  
  11.     public abstract class SessionAwareHandler : IHttpHandler, IRequiresSessionState  
  12.     {  
  13.            
  14.          .....  
  15.   
  16.         public void ProcessRequest(HttpContext context)  
  17.         {  
  18.             context.Response.ContentType = "text/plain";  
  19.               
  20.             // 身份认证  
  21.               
  22.             // 权限控制  
  23.   
  24.             MyProcess(context);  
  25.   
  26.         }  
  27.   
  28.        ........  
  29.   
  30.   
  31.       protected abstract void MyProcess(HttpContext context);  
  32.   
  33.      }  
  34. }  

一般处理程序实现类:

[csharp]  view plain copy print ?
  1. using System;  
  2. using System.Web;  
  3.   
  4. namespace SRERC.Web.admin  
  5. {  
  6.     /// <summary>  
  7.     /// bandHandler 的摘要说明  
  8.     /// </summary>  
  9.     public class bandHandler : SessionAwareHandler  
  10.     {  
  11.         protected override void MyProcess(HttpContext context)  
  12.         {  
  13.              //....处理过程  
  14.          //....  
  15.              context.Response.Write(json);  
  16.     
  17.         }  
  18.   
  19.     }  
  20. }  
參考文章2  stackoverflow

原文鏈接

原文中採納的回復就是正解

Implement the System.Web.SessionState.IRequiresSessionState interface

public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
{   
  public void ProcessRequest(HttpContext context)  
  {      
    context.Session["StackOverflow"] = "overflowing";      
    context.Response.Redirect("~/AnotherPage.aspx");      
  }

}
OK,能在handler中使用session了。但是 不知道是否符合規矩






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值