[C#]Cookie中保存Object类型数据[转]

对于Web中Object类型数据的保存,一般都是用Session,ViewState.不过这样一来,用户退出或者关闭IE后,就无法保存了,没办法,只能用Cookie试试了

原理:利用序列化/反序列化,保存Object类型的时候,序列化为字符串保存,取用的时候,再反序列化为对象。

Cookie管理核心代码:

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace CommonLibrary.CookieManager
{
    
public class ObjectListCookie
    {
        
public ObjectListCookie()
        {
            
//
        }
        
/// <summary>
        
/// 将Object类型添加到Cookie项的子项中
        
/// </summary>
        
/// <param name="CookieName">保存Cookie项名字</param>
        
/// <param name="CookieItem">要添加到子项的对象</param>
        
/// <param name="MaxCount">最大项数,0代表不限</param>
        
public void AddCookieItem(string CookieName, Object CookieItem, int MaxCount)
        {
           HttpCookie cookie;
            
if (HttpContext.Current.Request.Cookies[CookieName] != null)
            {
                cookie 
= HttpContext.Current.Request.Cookies[CookieName];
            }
            
else
            {
                cookie 
= new HttpCookie(CookieName);
            }

            
//保证不超过最大项数

            
if (cookie.Values.Count >= MaxCount)
            {
                
int DelCount = cookie.Values.Count - MaxCount + 1;
                
for (int i = 0; i < DelCount; i++)
                {
                    cookie.Values.Remove(cookie.Values.GetKey(
0));
                }
            }
            
string cookieValue = ObjectStrConvert.ObjectToBase64Str(CookieItem);
            cookie.Values.Add(System.DateTime.Now.ToString(
"yyyyMMddHHmmssfff"), cookieValue);
            cookie.Expires 
= DateTime.MaxValue;    
            
if (HttpContext.Current.Request.Cookies[CookieName] != null)
            {
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
           
else
            {
                HttpContext.Current.Response.Cookies.Add(cookie);
           }
        }

        
/// <summary>
        
/// 得到指定Cookie项中保存的对象列表
        
/// </summary>
        
/// <param name="CookieName">Cookie项名字</param>
        
/// <param name="ItemObjType">子项对象类型</param>
        
/// <returns></returns>
        
public List<Object> GetObjectListFromCookie(string CookieName, Type ItemObjType)
        {
            HttpCookie cookie;
            List
<Object> list = new List<Object>();
            
if (HttpContext.Current.Request.Cookies[CookieName] != null)
            {
                cookie 
= HttpContext.Current.Request.Cookies[CookieName];
                
for (int i = 0; i < cookie.Values.Count; i++)
                {
                    Object obj 
= ObjectStrConvert.Base64StrToObject(cookie.Values[i], ItemObjType);
                    list.Add(obj);
                }
            }        

            
return list;
        }

        
/// <summary>
        
/// 清除指定Cookie的所有子项
        
/// </summary>
        
/// <param name="CookieName">Cookie名字</param>

        
public void ClearCookieItems(string CookieName)
        {
            
if (HttpContext.Current.Request.Cookies[CookieName] != null)
            {
                HttpCookie cookie 
= HttpContext.Current.Request.Cookies[CookieName];
                cookie.Values.Clear();
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
        }
    }
}

 

posted on 2009-06-13 09:24  zeroStart  阅读(1452)  评论(0编辑  收藏  举报

导航