【问题标题】:Save object in Session on a ashx handler and get the object in another ashx handler. How?将对象保存在 ashx 处理程序上的 Session 中,并在另一个 ashx 处理程序中获取对象。如何?
【发布时间】:2012-11-25 15:35:02
【问题描述】:

我有两个处理程序。

在 Authentication.ashx 处理程序中,我在会话中保存一个对象,如下所示:

HttpContext.Current.Session["ConnectionUserSession"] = userCache;

然后,我有另一个处理程序,Send.ashx,我想获取保存在 Authentication.ashx 中的对象。

我是怎么做到的? 我获取对象的代码是:

UserCache userCache = (UserCache) HttpContext.Current.Session["ConnectionUserSession"];

问题: userCache 始终为 null,并且我在两个处理程序上都实现了 IRequiresSessionState。

【问题讨论】:

  • 抱歉,问题出在 Send.ashx 上。变量 userCache 始终为空。而且我在两个处理程序上都实现了接口 IRequiresSessionState。
  • 使用什么客户端技术来访问这个?
  • Connection是什么对象?在 Session 中放置任何非敏捷对象(包含数据库连接、打开文件句柄等资源的对象)是不好的做法。
  • 执行如何从一个处理程序传递到另一个处理程序?会话 ID cookie 是否被正确保留?
  • 所以,我有两个 aspx 页面。在 Default.aspx 中,我调用 Authentication.ashx 并将对象 UserCache 保存在 Session 中。我返回 Default.aspx,转到 SecondPage.aspx 并再次调用 Send.ashx。在 Send.ashx 上,我想获取保存在会话中的值以获取 UserCache。

标签: c# asp.net session httpcontext ashx


【解决方案1】:

任一行

HttpContext.Current.Session["ConnectionUserSession"] = userCache;

没有执行,或者保存和检索的字符串key不同,或者你有调用

Session.Abandon();

在这两个处理程序之间。

【讨论】:

    【解决方案2】:

    我认为他们的意思是在 Authentication.ashx 中:

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

    在 Send.ashx 中,

    public void ProcessRequest (HttpContext context) {
        UserCache userCache = (UserCache) context.Session["ConnectionUserSession"];
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2010-11-08
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-16
      • 2012-11-26
      相关资源
      最近更新 更多