ASP.NET: How to access Session from handler? [duplicate]

馋奶兔 提交于 2019-11-26 10:57:16

问题


i\'m trying to store some values in the Session from a Handler page, before i do a redirect to a WebForms page, that will pick up the Session values and pre-fill the WebForm:

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

Except context.Session object is null.

How do i access Session state from a handler?


回答1:


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

}



回答2:


Implement IRequiresSessionState




回答3:


Does implementing iRequiresSessionState resolve this?

What about doing an IHttpModule instead and overriding BeginRequest?

    public void Init(HttpApplication application)
    {
        application.BeginRequest += new EventHandler(context_BeginRequest);
    }


来源:https://stackoverflow.com/questions/1058568/asp-net-how-to-access-session-from-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!