【问题标题】:c# how to set up and use session state from preinitc# 如何从 preinit 设置和使用会话状态
【发布时间】:2013-11-08 17:51:02
【问题描述】:

可以这样设置和读取当前会话中的变量

String Myvar =(string) System.Web.HttpContext.Current.Session[“MyVariable”]

设置

System.Web.HttpContext.Current.Session[“MyVariable”] = “NewValue”

我两者都做不到,我从 System.Web.HttpContext.Current.Session 得到一个System.NullReferenceException: Object reference not set to an instance of an object.

在我的 web.config 我有

<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" cookieless="false" timeout="20">
</sessionState>  

我已经阅读了十几篇关于IHttpHandlerIRequiresSessionState 接口的必要性的文章。我认为问题可能是因为我在Page_PreInit 中请求此信息。我在一篇堆栈溢出文章中找到了 solution,但我似乎没有正确使用它来真正实现这一目标。

我不确定我错过了什么。提前致谢。

【问题讨论】:

  • 如果您将该代码移到 Page_Load 事件中,它会起作用吗?为什么在 PreInit 中需要这个?
  • 确保您的 ASP .Net State 服务已启动。即使您将代码放在 Page_PreInit 中,它也应该可以工作。你能提供更多信息吗?
  • 还有一件事,您首先使用的是哪条线路。如果您没有设置 session["MyVariable"] 并且您尝试转换为(字符串)。它会给你 NULL 参考错误。所以尝试在强制转换之前放置 NULL 检查条件
  • 我正在交换母版页 - 在 PreInit 上是我知道如何做到这一点的唯一地方。

标签: c# asp.net session-state


【解决方案1】:

正如评论中提到的,您在PreInit 事件中需要这个有什么原因吗?

PreInit 发生在页面生命周期的早期。事实上,它甚至在应用母版页(如果有)之前,在所有控件完全初始化之前,等等。

Load 事件是对大多数应用程序更好的选择。如果您仍然在那里收到NullReferenceException,那么问题就更大了。

【讨论】:

  • 我想我确实需要在 PreInit 中进行。那时我不能访问状态吗?我知道我可以通过我在问题中链接的解决方案。不幸的是,我错过了实现它所需的一小部分难题。
  • 对于交换母版页,是的,您需要在 PreInit 进行。但是,交换母版页对您来说依赖于会话状态吗?除了会话状态之外,还有其他机制可以用来获取交换母版页所需的信息吗?
  • 它基于登录时收集的用户管理员级别。我想我可以通过帖子和隐藏变量将信息从一个页面传递到另一个页面,但是......糟糕。我会保存我所有的用户信息并在史前传递它。
【解决方案2】:

您可以通过在您的类中实现 IRequiresSessionState 接口来访问会话。

这是一个标志接口,所以你不需要实现任何额外的代码。

当你实现这个时,asp.net 会知道你想要访问会话。

public partial class YOUR_ASPX: System.Web.UI.Page , IRequiresSessionState
{

... your code

}

【讨论】:

    【解决方案3】:

    要访问会话状态预初始化,您可以执行以下操作。我使用它是为了拥有与普通用户不同的管理员管理员。每个页面顶部都有一个方法。

    PageTools tools = new PageTools();
    protected void Page_PreInit(object sender, EventArgs e)
    {
        tools.setMasterPage(Page, Context);
    }
    

    PageTools 是我的类,它包含选择适当母页并具有 http 处理程序的方法。

    public void setMasterPage(Page page, HttpContext context)
        /***********************************************************************
         * Author   Daniel Tweddell
         * Date     9/18/09
         * 
         * Several of the pages are for non-admin use, however these pages will
         * also be used by the admin users and will need to have the admin menu
         * and such.  So based on the login, we either show the page with the
         * standard master or if the user is admin, use the admin master.
         ***********************************************************************/
        {
            if (context.Handler is IReadOnlySessionState || context.Handler is IRequiresSessionState)
            {
                context.Handler = Handler();
            }
            String sMasterPage="~/content/page.master";
            if (userinfo.IsUserAdmin) sMasterPage="~/content/administrator/admin.master";//make sure the user is admin
            page.MasterPageFile = sMasterPage; 
        }
    

    Here是一步一步来设置httphandler的。 (这是您需要的另一件事。

    【讨论】:

      猜你喜欢
      • 2018-11-03
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 2013-01-07
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      相关资源
      最近更新 更多