互换的青春
参考代码如下://如果请求的Cookie对象为空if (Request.Cookies["userCookie"] == null){//创建一个Cookie对象HttpCookie userCookie = new HttpCookie("userCookie");//给对象赋值userCookie.Values["userName"] =userInfo.UserName.ToString();userCookie.Values["lastVist"] =DateTime.Now.ToString();userCookie.Values["count"] = "1";//设置对象的过期时间userCookie.Expires = DateTime.Now.AddDays(30);//添加到Cookies集合Response.Cookies.Add(userCookie);}//不为空的情况else{int counter =Convert.ToInt32(Request.Cookies["userCookie"]["count"]) + 1;HttpCookie userCookie =new HttpCookie("userCookie");userCookie.Values["userName"] =userInfo.UserName.ToString();userCookie.Values["lastVist"] =DateTime.Now.ToString();userCookie.Values["count"] =counter.ToString();userCookie.Expires =DateTime.Now.AddDays(30);Response.Cookies.Add(userCookie);}