把多条数据序列化存储在cookie并取出
1 namespace shendeng.lottery.helper 2 { 3 public class ControllerHelper 4 { 5 /// <summary> 6 /// 取出公告 7 /// </summary> 8 /// <returns></returns> 9 public static List<New> GetNewList() 10 { 11 List<New> list = null; 12 System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); 13 //如果cookie里有值就直接取 14 if (System.Web.HttpContext.Current.Request.Cookies["news"] != null) 15 { 16 string strNews = System.Web.HttpContext.Current.Request.Cookies["news"].Value; 17 if (!string.IsNullOrEmpty(strNews)) 18 { 19 list = jss.Deserialize<List<New>>(strNews); 20 } 21 } 22 else 23 { 24 //lotteryDataContex datas = new DatabaseFactory().Get(); 25 //公告类型 26 Guid newtypeId = Guid.Parse("6029379D-06D0-42E8-8DE6-872B4A621B65"); 27 list = new NewRepository(new DatabaseFactory()).GetTopManyOrderBy(n => n.NewTypeId == newtypeId && n.StartTime < DateTime.Now && n.EndTime > DateTime.Now && n.State != 9 && n.Enabled == true, n => n.CreateTime, 5, true).ToList(); 28 //序列化存入cookie 29 string serList = jss.Serialize(list); 30 HttpCookie hc = new HttpCookie("news", serList); 31 hc.Expires = DateTime.Now.AddHours(2); 32 System.Web.HttpContext.Current.Response.Cookies.Add(hc); 33 } 34 return list; 35 } 36 37 38 39 } 40 }