解决在构造函数中使用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

 

转载于:https://www.cnblogs.com/servant/p/5369110.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值