C#如何设置和preINIT使用会话状态 [英] c# how to set up and use session state from preinit

查看:101
本文介绍了C#如何设置和preINIT使用会话状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定这样设置和读取从当前的会话变量

OK so to set and read variables from the current session

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

要设置

System.Web.HttpContext.Current.Session["MyVariable"] = "NewValue"

我能做到两者都不是,我收到了 System.NullReferenceException:对象不设置到对象的实例从System.Web.HttpContext.Current.Session。

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.

在我的web.config我有

In my web.config I have

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

我已经阅读的IHttpHandler IRequiresSessionState 接口的必要性十几文章。我想是因为我请求在 Page_ preINIT 此信息的问题可能引起的。我发现了一个 <一个href=\"http://stackoverflow.com/questions/276355/can-i-access-session-state-from-an-httpmodule\">solution堆栈溢出的文章中,但我似乎并没有使用它进行适当真正让这个去了。

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.

推荐答案

要访问会话状态pre-INIT你可以做这样的事情。我用它,这样我可以比普通用户一个不同的管理大师。每个页面的顶部有一个方法。

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是我的类,它认为,选择适当的母校页,有HTTP处理程序的方法。

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

<一个href=\"http://stackoverflow.com/questions/1375627/irequiressessionstate-how-do-i-use-it\">Here是一步步来建立的HttpHandler。 (这是你所需要的其他东西。

Here is a step by step to setting up the httphandler. (which is the other thing you'll need.

这篇关于C#如何设置和preINIT使用会话状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆