我有一个方法public BlogVO Load(int id){}
BlogVO是一个类
public class BlogVO
{
private int blogID;
private string username;
                  ............
public int BlogID
{
get{ return blogID;}
set{ blogID = value;}
}
public string Username
{
get{ return username;}
set{ username = value;}
}
           ......................
        }
如何把方法赋给Response.Cookies["aaa"](不用Session和ViewState),
Response.Cookies["aaa"].Value = bDAO.Load(back).ToString();
或者这样Response.Cookies["aaa"] = bDAO.Load(back);
,都会会出错
我该怎么办啊

解决方案 »

  1.   

                HttpCookie cookie2 = new HttpCookie("mycookie", "34534534534534534");
                Response.Cookies.Add(cookie2);
      

  2.   

               HttpCookie cookie2 = new HttpCookie("mycookie");
                cookie2 .Value = "34534534534534534";
                Response.Cookies.Add(cookie2);
      

  3.   

    我是要给cookies赋个很特殊的值,这个值就是上面我写的那个类呀@!
    可不是一个很简单的值
      

  4.   

    复杂的数据类型是不能直接存贮到Cookie中的,Cookie中只能存贮字符串。可以复杂的数据类型转换成多个字符串,然后把这多个字符串同时赋值给产生的Cookie值
      

  5.   

    Response.Cookies["aaa"].Value = bDAO;
    ..............................
    .............................
      

  6.   

    Cookie里只能存放字符串,而且大小限制在4K
      

  7.   

    把你的类转成字符串.
    比如:
    string temp="public class BlogVO { }";
    HttpCookie cookie = new HttpCookie("mycookie", "temp"); 
    Response.Cookies.Add(cookie);
      

  8.   

    可以序列化该类为字符串
     MemoryStream stream = new MemoryStream();
               BinaryFormatter formatter = new BinaryFormatter();
               formatter.Serialize(stream, obj);
               return Convert.ToBase64String(stream.ToArray());
    在需要的时候反序列化
    BinaryFormatter formatter = new BinaryFormatter();
               MemoryStream stream = new MemoryStream(Convert.FromBase64String(str));
               return formatter.Deserialize(stream);
      

  9.   

    那能不能把那个方法返回的类,转换成字符窜赋给Cookie,如何做啊,我在网上找了一些,不行
      

  10.   

    序列化/反序列化,为了便于在Cookie中保存,做了一个Base64编码。该文章转载自网络大本营:http://www.xrss.cn/Dev/DotNet/20082218665.Html  
    如下: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);
                }
            }
        }
    }该文章转载自网络大本营:http://www.xrss.cn/Dev/DotNet/20082218665.Html
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml.Serialization;
    using System.Xml;
    using System.IO;namespace CommonLibrary.CookieManager
    ...{
        public class ObjectStrConvert
        ...{
            /**//// <summary>
            /// 对象序列化,得到Base64字符串
            /// </summary>
            /// <param name="Obj">待序列化的对象</param>
            /// <returns></returns>
            public static string ObjectToBase64Str(Object Obj)
            ...{
                return Convert.ToBase64String(ObjToBytes(Obj));
            }        /**//// <summary>
            /// Base64字符串反序列化,得到对象
            /// </summary>
            /// <param name="ScrStr">待反序列化的字符串</param>
            /// <param name="ObjType">对象类型</param>
            /// <returns></returns>
            public static Object Base64StrToObject(string ScrStr, Type ObjType)
            ...{
                return BytesToObj(Convert.FromBase64String(ScrStr), ObjType);
            }        /**//// <summary>
            /// 对象序列化,得到字符串
            /// </summary>
            /// <param name="Obj">待序列化的对象</param>
            /// <returns></returns>
            public static string ObjectToString(Object Obj)
            ...{
                return Encoding.Default.GetString(ObjToBytes(Obj));
            }        /**//// <summary>
            /// 字符串反序列化,得到对象
            /// </summary>
            /// <param name="ScrStr">待反序列化的字符串</param>
            /// <param name="ObjType">对象类型</param>
            /// <returns></returns>
            public static Object StringToObject(string ScrStr, Type ObjType)
            ...{
                return BytesToObj(Encoding.Default.GetBytes(ScrStr), ObjType);
            }
            
            /**//// <summary>
            /// 将对象序列化为字节数组
            /// </summary>
            /// <param name="Obj"></param>
            /// <returns></returns>
            private static byte[] ObjToBytes(Object Obj)
            ...{
                XmlSerializer ser = new XmlSerializer(Obj.GetType());
                MemoryStream mem = new MemoryStream();
                XmlTextWriter writer = new XmlTextWriter(mem, Encoding.Default);
                ser.Serialize(writer, Obj);
                writer.Close();            return mem.ToArray();
            }        /**//// <summary>
            /// 从字节数组反序列化得到对象
            /// </summary>
            /// <param name="ObjBytes"></param>
            /// <returns></returns>
            private static Object BytesToObj(byte[] ObjBytes, Type ObjType)
            ...{
                try
                ...{
                    XmlSerializer ser = new XmlSerializer(ObjType);
                    MemoryStream mem = new MemoryStream(ObjBytes);
                    StreamReader sr = new StreamReader(mem, Encoding.Default);
                    return ser.Deserialize(sr);
                }
                catch
                ...{
                    return null;
                }
            }
        }
    }该文章转载自网络大本营:http://www.xrss.cn/Dev/DotNet/20082218665.Html
      

  11.   

    public BlogVO Load(int id){} 这方法返回的是BlogVO对象..Response.Cookies["aaa"].Value = bDAO.Load(back).ToString(); 
    或者这样Response.Cookies["aaa"] = bDAO.Load(back); 你这样写都不对不能放到cookie中要想存.好像得序列化.试试11楼的