您的位置:首页 > 编程语言 > ASP

在asp.net handler 中 使用 session

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

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



下面是網友的文章:

參考文章1

原文鏈接

抽象类

[csharp] view
plaincopyprint?

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);  

  

     }  

}  

一般处理程序实现类:

[csharp] view
plaincopyprint?

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);  

    

        }  

  

    }  

}  

參考文章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了。但是不知道是否符合規矩
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  asp.net session handler