【问题标题】:ASP.NET: How to access Session from handler? [duplicate]ASP.NET:如何从处理程序访问 Session? [复制]
【发布时间】:2009-06-29 14:18:10
【问题描述】:

我是 trying to store some values in the Session from a Handler page,在我重定向到 WebForms 页面之前,它将获取 Session 值并预填充 WebForm:

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

除了context.Session 对象为空。

如何从处理程序访问会话状态?

【问题讨论】:

    标签: asp.net session session-state handler ihttphandler


    【解决方案1】:

    实现System.Web.SessionState.IRequiresSessionState接口

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

    【讨论】:

    • 注意:您不必实际实现任何东西,只需将接口添加到您的类中即可。然后,网络服务器会看到您正在请求它,并填写它。
    • 是的,它仍在实现接口,但由于它是一个标记接口,因此除了接口的派生之外,我们不需要编写任何代码。
    • 由于某种原因,即使指定了IRequiresSessionState,我的也无法正常工作。我不得不使用IReadOnlySessionState。我还没有研究为什么,但它正在工作..
    【解决方案2】:

    实现IRequiresSessionState

    【讨论】:

      【解决方案3】:

      实施iRequiresSessionState 能解决这个问题吗?

      改用 IHttpModule 并覆盖 BeginRequest 怎么样?

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

      【讨论】:

      • 有谁知道哪个性能更好?
      • 我遇到了同样的问题,我在处理程序代码中使用了 iRequiresSessionState,但我仍然无法访问会话值。会话密钥仍为 0。谁能告诉我这个?
      猜你喜欢
      • 1970-01-01
      • 2012-08-11
      • 2010-11-08
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      • 2020-06-16
      相关资源
      最近更新 更多