首先 handler 默認情況下是不支持 session 的。如果想在一般處理程序(handler)中使用session ,需要實現一個不需要實現任何方法的接口IRequiresSessionState
引入 System.Web.SessionState 即可。此時需要context.Session["pars"]這樣引用。具體實例如下圖所示:
下面是網友的文章:
參考文章1
原文鏈接
抽象类
- using System;
- using System.Web;
- using System.Web.SessionState;
- ....
- namespace SRERC.Web.admin
- {
- /// <summary>
- /// SessionAwareHandler 的摘要说明
- /// </summary>
- public abstract class SessionAwareHandler : IHttpHandler, IRequiresSessionState
- {
- .....
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
- // 身份认证
- // 权限控制
- MyProcess(context);
- }
- ........
- protected abstract void MyProcess(HttpContext context);
- }
- }
一般处理程序实现类:
- using System;
- using System.Web;
- namespace SRERC.Web.admin
- {
- /// <summary>
- /// bandHandler 的摘要说明
- /// </summary>
- public class bandHandler : SessionAwareHandler
- {
- protected override void MyProcess(HttpContext context)
- {
- //....处理过程
- //....
- context.Response.Write(json);
- }
- }
- }
原文鏈接
原文中採納的回復就是正解
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了。但是
不知道是否符合規矩