2009-06-29 33 views
47

我是trying to store some values in the Session from a Handler page,我做一个重定向到一个WebForms的页面之前,将拿起会议值和预填的WebForm:ASP.NET:如何从处理程序访问会话?

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

除了context.Session对象为null。

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

回答

104

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

} 
+0

注意:您不必实际执行任何操作,只需将该接口添加到您的类中即可。网络服务器然后看到你在问它,并填充它。 – 2009-06-29 17:35:47

+3

是的,它仍然在实现接口,但由于它是一个标记接口,没有任何代码我们不得不编写其他接口的派生。 – JoshBerke 2009-06-29 20:33:13

10

实施IRequiresSessionState

7

是否实施iRequiresSessionState解决呢?

那么做一个IHttpModule而不是重写BeginRequest呢?

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