【问题标题】:Asp.Net Session is null in ashx fileAsp.Net 会话在 ashx 文件中为空
【发布时间】:2011-01-10 12:31:20
【问题描述】:

我正在尝试访问 Asp.Net ashx 处理程序中的 Session 变量,如下所示。

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

context.Session 在上述方法中始终为 Null。如何访问 ashx 文件中的 Session 对象?

【问题讨论】:

    标签: asp.net session httphandler


    【解决方案1】:

    您必须“实现”IRequiresSessionStateIReadOnlySessionState,前者提供对会话的完全访问权限,而后者提供只读访问权限。

    我在这里引用“实现”是因为这两个是所谓的“marker interfaces”,这意味着它们没有成员。

    【讨论】:

    • 赞一个好先生。我唯一的遗憾是我只能给一个。
    • 太棒了!至于“标记接口”,如果他们把它做成Annotations不是更好吗?
    • “实现”Anton 的意思是:公共类 ClassName : IHttpHandler, System.Web.SessionState.IRequiresSessionState{...}
    【解决方案2】:

    在 VB 中,像这样实现 Anton 提到的接口(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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      • 2011-05-31
      • 1970-01-01
      相关资源
      最近更新 更多