2009-06-29 66 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); 
    }