C# 如何从 preinit 设置和使用会话状态

发布于 2024-08-03 02:20:29 字数 837 浏览 2 评论 0原文

好的,从当前会话中设置和读取变量

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

要设置,

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

我两者都不能做,我从 System.Web.HttpContext 得到一个 System.NullReferenceException:对象引用未设置为对象的实例。。当前.会话。

在我的 web.config 中,我

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

阅读了十几篇关于 IHttpHandler 和 IRequiresSessionState 接口的必要性的文章。我认为该问题可能是因为我在 Page_PreInit 中请求此信息而引起的。我在中找到了解决方案一篇堆栈溢出文章,但我似乎没有正确使用它来实际实现这一点。

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

OK so to set and read variables from the current session

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

To set

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

I can do neither, I get a System.NullReferenceException: Object reference not set to an instance of an object. from System.Web.HttpContext.Current.Session.

In my web.config I have

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

I have read a dozen articles on the the necessity of IHttpHandler and an IRequiresSessionState interface. I think the issue may be caused because I am requesting this information in Page_PreInit. I found a solution in a stack overflow article but I don't seem be using it properly to actually make this go.

I am not sure what I am missing. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路还长,别太狂 2024-08-10 02:20:29

正如评论中提到的,是否有某种原因需要在 PreInit 事件中使用它?

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

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

As the comment mentioned, is there some reason you need this in the PreInit event?

PreInit happens very early in the page lifecycle. It happens, in fact, before even the master page (if any) is applied, before all the controls are fully initialized, etc.

A much better choice for the majority of applications is in the Load event. If you're still getting a NullReferenceException there, then there's a bigger issue.

寄居者 2024-08-10 02:20:29

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

这是一个标志接口,因此您不需要实现任何额外的代码。

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

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

... your code

}

You can access the session by implementing IRequiresSessionState interface in your class.

This is a flag interface, so you dont need to implement any extra code.

When you implement this asp.net will know that you want to access the session.

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

... your code

}
那支青花 2024-08-10 02:20:29

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

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; 
    }

此处是设置步骤http处理程序。 (这是您需要的另一件事。

To access the session state pre-init you can do something like this. I use it so that I can have a different admin master than the regular user one. Each page has a method at the top.

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

PageTools is my class that holds the method that chooses the appropriate mater page and has the http handler.

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 is a step by step to setting up the httphandler. (which is the other thing you'll need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文