2010-01-28 92 views
31

我想在Asp.Net ashx處理程序中訪問會話變量,如下所示。Asp.Net會話在ashx文件中爲空

public void ProcessRequest (HttpContext context) { 
     context.Session["VariableName"] = Id; 
    } 

context.Session總是上述方法的內部。如何訪問ashx文件中的會話對象?

回答

94

您必須「執行」IRequiresSessionStateIReadOnlySessionState,前者提供完全訪問會話,後者提供只讀訪問權限。

我在這裏引用「實施」,因爲這兩個是所謂的「marker interfaces」,這意味着他們沒有成員。

+2

的感謝!也需要這個 – Michel 2011-03-23 09:08:28

+3

有一個upvote好先生。我唯一的遺憾是我只能給一個。 – CountMurphy 2011-10-17 20:46:34

+0

太棒了!而關於「標記界面」,如果他們將它作爲註釋,會不會更好? – everton 2011-12-22 12:54:21

2

在VB中,實現由安東(IRequiresSessionState或IReadOnlySessionState)這樣提到的接口:

Public Class MyAshxFile 

    Implements System.Web.IHttpHandler 
    Implements System.Web.SessionState.IRequiresSessionState ''need this for session variables 
    Implements System.Web.SessionState.IReadOnlySessionState ''need this for session variables