asp.net 使用cookies或者session实现浏览历史记录功能

cooies实现方式:

读取cookies存储数据

    /// <summary>
    ///HistoryRestore 的摘要说明
    ///最近浏览记录
    /// </summary>
    public class HistoryRestore
    {
        public HistoryRestore()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        public static void HistoryRestoreList(string cookieName, int objectID)
        {
            HttpRequest Request = HttpContext.Current.Request;
            HttpResponse Response = HttpContext.Current.Response;

            if (Request.Cookies[cookieName] != null)
            {
                HttpCookie tempCurBuyerList = Request.Cookies[cookieName];
                string tempstr = tempCurBuyerList.Value;
                if (tempstr.IndexOf(",") > 0)
                {
                    string[] sArray = tempstr.Split(',');
                    bool hasthis = false;

                    foreach (string s in sArray)
                    {
                        if (s == objectID.ToString())
                        {
                            hasthis = true;
                            break;
                        }
                        else
                        {
                            hasthis = false;
                        }
                    }

                    if (!hasthis)   //如果没有ID,则加入
                    {
                        if (sArray.Length > 11) //3为存储浏览记录数的数量,实际数量为7
                        {
                            // 超过数量,去掉最先入队的元素
                            tempstr = tempstr.Substring(0, tempstr.LastIndexOf(","));
                        }
                        // 队列
                        tempstr = objectID.ToString() + "," + tempstr;
                    }
                }
                else
                {
                    //tempstr += "," + objectID.ToString();  
                    if (tempstr != objectID.ToString())
                    {
                        tempstr = objectID.ToString() + "," + tempstr;
                    }
                }
                tempCurBuyerList.Value = tempstr;
                tempCurBuyerList.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(tempCurBuyerList);
                //或者 Response.Cookies[cookieName].Value = tempstr;
            }
            else
            {
                HttpCookie addToCookies = new HttpCookie(cookieName);
                addToCookies.Value = objectID.ToString();
                addToCookies.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(addToCookies);
            }
        }
    }

在用户浏览某产品时记录到cookies中:

            HistoryRestore.HistoryRestoreList("restoreid", int.Parse(pid));

读取cookies存储数据

       List<VCtuan.Model.Tb_Product> proList = null;
        if (Request.Cookies["restoreid"] != null)
        {
            HttpCookie tempCurBuyerList = Request.Cookies["restoreid"];

            string[] strArr = tempCurBuyerList.Value.Split(',');
            proList = new List<VCtuan.Model.Tb_Product>();

            foreach (string s in strArr)
            {
                VCtuan.Model.Tb_Product model = proBLL.GetModel(int.Parse(s)); //商品的实体类
                if (model != null)
                {
                    proList.Add(model);
                }
            }
        }
        RptLastProductList.DataSource = proList;
        RptLastProductList.DataBind();

http://www.cnblogs.com/woshilee/articles/2391722.html


使用session方式:

        List<string> list = null;
        if (Session["track"] == null)
        {
            list = new List<string>();
        }
        else
        {
            list = (List<string>)Session["track"];
        }

        if (list.Contains(pid))
            list.Remove(pid);    // 如果这次浏览的商品在浏览记录中,删除后重新添加进去,保持浏览顺序
        if (list.Count == 10)
            list.RemoveAt(0);    // 浏览记录保存10个,到了10个删除最老的一个记录.
        list.Add(pid);           // 浏览顺序为list[9] list[8].....list[1] list[0]
        Session["track"] = list;    // 把list更新到Session 


        ///
        if (list.Count > 0)
        {
            List<VCtuan.Model.Tb_Product> proList = new List<VCtuan.Model.Tb_Product>();
            for (int i = list.Count - 1; i >= 0; i--)
            {
                string proId = list[i];
                proList.Add(proBLL.GetModel(int.Parse(proId)));
            }
            RptLastProductList.DataSource = proList;
            RptLastProductList.DataBind();
        }

http://zhidao.baidu.com/question/199885679.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值