ASP.net HttpContext.Current.Session包装器System.NullReferenceException

时间:2017-08-14 13:02:45

标签: c# asp.net asp.net-mvc session httpcontext

嘿所有我试图建立一个会话,每当我从一页到另一页时都不会变成NULL。

我在Stackoverflow上找到了THIS页面,它是我目前正在努力工作的那个页面。 @ M4N的帖子就是我所说的。

我的代码是:

HELPer.cs:

public class SessionWrapper
{
    private SessionWrapper()
    {
    }

    public static SessionWrapper Instance
    {
        get
        {
            SessionWrapper wrapper = (SessionWrapper)HttpContext.Current.Session["SESSION_MANAGER"];

            if (wrapper == null)
            {
                wrapper = new SessionWrapper();
                HttpContext.Current.Session["SESSION_MANAGER"] = wrapper;
            }

            return wrapper;
        }
    }

    public string _lastFirstMiddle { get; set; }
    public string _company { get; set; }
    public string _userPrincipalName { get; set; }
    public string _sAMAccountName { get; set; }
    public string _displayName { get; set; }
    public string _department { get; set; }
    public string _departmentNumber { get; set; }
    public string _physicalDeliveryOfficeName { get; set; }
    public string _division { get; set; }
    public string _employeeID { get; set; }
    public string _givenName { get; set; }
    public string _mail { get; set; }
    public string _name { get; set; }
    public string _sAMAccountType { get; set; }
    public string _sn { get; set; }
    public string _title { get; set; }
    public int _userID { get; set; }
    public string _userAMT { get; set; }
    public List<string> _perms { get; set; }

    public static string lastFirstMiddle { get { return Instance._lastFirstMiddle; }set { Instance._lastFirstMiddle = value; }}
    public static string company { get { return Instance._company; } set { Instance._company = value; } }
    public static string userPrincipalName { get { return Instance._userPrincipalName; } set { Instance._userPrincipalName = value; } }
    public static string sAMAccountName { get { return Instance._sAMAccountName; } set { Instance._sAMAccountName = value; } }
    public static string displayName { get { return Instance._displayName; } set { Instance._displayName = value; } }
    public static string department { get { return Instance._department; } set { Instance._department = value; } }
    public static string departmentNumber { get { return Instance._departmentNumber; } set { Instance._departmentNumber = value; } }
    public static string physicalDeliveryOfficeName { get { return Instance._physicalDeliveryOfficeName; } set { Instance._physicalDeliveryOfficeName = value; } }
    public static string division { get { return Instance._division; } set { Instance._division = value; } }
    public static string employeeID { get { return Instance._employeeID; } set { Instance._employeeID = value; } }
    public static string givenName { get { return Instance._givenName; } set { Instance._givenName = value; } }
    public static string mail { get { return Instance._mail; } set { Instance._mail = value; } }
    public static string name { get { return Instance._name; } set { Instance._name = value; } }
    public static string sAMAccountType { get { return Instance._sAMAccountType; } set { Instance._sAMAccountType = value; } }
    public static string sn { get { return Instance._sn; } set { Instance._sn = value; } }
    public static string title { get { return Instance._title; } set { Instance._title = value; } }
    public static int userID { get { return Instance._userID; } set { Instance._userID = value; } }
    public static string userAMT { get { return Instance._userAMT; } set { Instance._userAMT = value; } }
    public static List<string> perms { get { return Instance._perms; } set { Instance._perms = value; } }
}

这就是我发起它的方式:

SessionWrapper.Instance._lastFirstMiddle = de.Properties["cn"].Value.ToString();
SessionWrapper.Instance._company = de.Properties["company"].Value.ToString();
SessionWrapper.Instance._userPrincipalName = de.Properties["userPrincipalName"].Value.ToString();
SessionWrapper.Instance._sAMAccountName = de.Properties["sAMAccountName"].Value.ToString();
SessionWrapper.Instance._displayName = de.Properties["displayName"].Value.ToString();
SessionWrapper.Instance._department = de.Properties["department"].Value.ToString();
SessionWrapper.Instance._departmentNumber = de.Properties["departmentNumber"].Value.ToString();
SessionWrapper.Instance._division = de.Properties["division"].Value.ToString();
SessionWrapper.Instance._employeeID = de.Properties["employeeID"].Value.ToString();
SessionWrapper.Instance._title = de.Properties["title"].Value.ToString();
SessionWrapper.Instance._givenName = de.Properties["givenName"].Value.ToString();
SessionWrapper.Instance._name = de.Properties["name"].Value.ToString();
SessionWrapper.Instance._sAMAccountType = de.Properties["sAMAccountType"].Value.ToString();
SessionWrapper.Instance._sn = de.Properties["sn"].Value.ToString();
SessionWrapper.Instance._physicalDeliveryOfficeName = de.Properties["physicalDeliveryOfficeName"].Value.ToString();
SessionWrapper.Instance._perms = allData.Tables[0].Rows[0]["permissions"].ToString().Split('|').ToList<string>();
SessionWrapper.Instance._userID = (int)allData.Tables[0].Rows[0]["id"];

但是,我收到了以下错误:

  

System.NullReferenceException:未将对象引用设置为对象的实例。

在这一行:

SessionWrapper wrapper = (SessionWrapper)HttpContext.Current.Session["SESSION_MANAGER"];

所以我不确定我需要做些什么才能纠正这个错误,所以任何帮助都会很棒!

[更新1]

enter image description here

0 个答案:

没有答案