日期:2014-05-17  浏览次数:20430 次

读取cookie值总是提示未被实例化

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.IsAuthenticated)
            {
                HttpCookie cookies = Request.Cookies["fastuser"];
                if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
                {
                    //username.Text  = Request.Cookies[FormsAuthentication.FormsCookieName]["receiver"].ToString();
                    username.Text = cookies.Values["receiver"].ToString();

                    //username.Text  = Request.Cookies[FormsAuthentication.FormsCookieName]["receiver"].ToString(); 
                    username.Text = cookies.Values["receiver"].ToString();

以上这两句不管用哪一句都提示“未将对象引用设置到对象的实例“,这是什么原因呢,我在百度里看别人都是直接这么写的,为什么我的就不对吗?新手求指导

------解决方案--------------------
HttpCookie myHc = Request.Cookies["fastuser"];
if (myHc != null)
  {
     username.Text = myHc .Values["receiver"];
  }
LZ试下 话说很久没用过这东西了
------解决方案--------------------
Refer:

http://www.cnblogs.com/insus/articles/2055310.html