小编Hen*_*nie的帖子

HttpContext.Current.Session - NullReferenceException

我一直在体验一个对象引用未设置为我的MVC 4 ASP.NET项目的Settings类中的对象错误的实例,它获取我的当前会话详细信息.每次我浏览一个页面变量抛出NullReferenceException异常,并不能明白为什么,因为它是没有任何问题的工作以前完善.

namespace TracerCRM.Web
{
    public class Settings
    {
        public static Settings Current
        {
            get
            {
                Settings s = HttpContext.Current.Session["Settings"] as Settings;
                if (s == null)
                {
                    s = new Settings();
                    HttpContext.Current.Session["Settings"] = s;
                }

                return s;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试过以下在研究中遇到的事情:

1:"HttpContext.Current.Session"vs Global.asax"this.Session"

2:罕见的System.NullReferenceException显示以前填充的HttpContext.Current.Session ["courseNameAssociatedWithLoggedInUser"]?

3:部署到IIS 7后,ASP.NET MVC 4 Web应用程序中的Session对象为空(W 2008 R2)

4:在ASP.NET MVC会话到期时关闭用户

5:在asp.net mvc 3中重定向到操作时,登录会话有时会丢失

以上都不适合我.

c# asp.net asp.net-mvc session asp.net-mvc-4

2
推荐指数
2
解决办法
9296
查看次数

标签 统计

asp.net ×1

asp.net-mvc ×1

asp.net-mvc-4 ×1

c# ×1

session ×1