How to use ASP.NET Session State in an HttpHandler?

后端 未结 4 921
心在旅途
心在旅途 2020-12-08 18:22

I have an HttpHandler that is run on a client page (cross domain, not on our IIS server, etc) and when they click on our embedded link it fires off the Handler on our server

相关标签:
4条回答
  • 2020-12-08 18:54

    Have your HttpHandler implement the IRequiresSessionState interface. It will enable session state use. IRequiresSessionState can be found in the System.Web.SessionState namespace.

    0 讨论(0)
  • 2020-12-08 18:58

    I think you have to implement the empty interface IReadOnlySessionState, so the context will be loaded.

    edit to add:

    According to Michael Morton's answer, you can also implement IRequiresSessionState, which will give you write access also to the Session object

    0 讨论(0)
  • 2020-12-08 18:59
    using System; 
    using System.Web;
    using System.Web.SessionState;
    public class DownloadHandler : IHttpHandler, IReadOnlySessionState
    {
       public bool IsReusable { get { return true; } }
    
       public void ProcessRequest(HttpContext context)
       {
           context.Response.Write(context.Session["kmx"]);
       }
    }
    
    0 讨论(0)
  • 2020-12-08 19:01

    try using the current context...

    System.Web.HttpContext.Current.Session
    
    0 讨论(0)
提交回复
热议问题