zoukankan      html  css  js  c++  java
  • 解决在构造函数中使用Session,Session为null的问题

    问题描述:

    public abstract class PageBase : System.Web.UI.Page

    在PageBase中如何使用Session???

    我直接用 Session["Name"]

    提示:只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 <configuration><system.web><httpModules> 节中包括 System.Web.SessionStateMod 或自定义会话状态模块。

    使用 HttpContext.Current.Session["User"] = "";

    提示:未将对象引用设置到对象的实例..

    如果在页面中使用(这个页面继承自PageBase)

    使用 Session["Name"] 或者 HttpContext.Current.Session["User"] = "";都没有问题

    我自己在web.config中设置SessionState为<sessionState mode="InProc" timeout="20"/>

    错误原因:在PageBase的构造函数中使用Session

    解决办法:

    按照Page的创建流程,,,造成了先使用了Session后面才创建Session
    现在重写了OnInit  在 base.OnInit后再使用Session 问题解决..

    protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                if (Session["crmService"] != null)
                {
                    return (IOrganizationService)Session["crmService"];
                }
                else
                {
                    CRMService = CrmServiceHelper.Instance.GetService();
                    if (_currentuser == null)
                    {
                        //获取当前登陆人
                        WhoAmIRequest request = new WhoAmIRequest();
                        WhoAmIResponse response = (WhoAmIResponse)CRMService.Execute(request);
                        Guid userId = response.UserId;
                        _currentuser = getUserById(userId.ToString());
                    }
                    Session["crmService"] = CRMService;
                    return GetCRMService();
                }
            }
    View Code
  • 相关阅读:
    厦门航空牵手阿里云打造航空业移动研发中台,研发效率提升50%
    可能是国内第一篇全面解读 Java 现状及趋势的文章
    这样才能正确解锁MaxCompute客户端
    MaxCompute问答整理之10月
    tensorflow入门
    buctoj——合法的出栈顺序
    nyoj299——如何优雅的写矩阵快速幂
    nyoj164——卡特兰数(待填坑)
    nyoj139——康托展开
    字符串练习
  • 原文地址:https://www.cnblogs.com/servant/p/5369110.html
Copyright © 2011-2022 走看看