获取最新显示的所有Cookie列表 [英] Get a list of all the cookies annd display the most recent

查看:102
本文介绍了获取最新显示的所有Cookie列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。

每当用户进入包含产品详情的页面时,我需要添加一个新的cookie,在该页面或任何类似页面中我需要显示最新的cookie(产品) 。

所以我需要的是:例如我有4个cookie,名称=产品名称和价值=产品的ID。

我需要得到所有4个cookie都比较它们的值或名称,并用html中的小div显示它们的属性。

我在Visual Studio中使用C#工作,我是新手。

提前谢谢你。



我尝试了什么:



这是我设置和获取cookie的代码。Recent.cs:

Hello.
I need to add a new cookie everytime a user enters a page with details about a product and in that page or any similar page i need to display the most recent cookies (Products).
So what i need is: For example i have 4 cookies with name = the name of the product and value = the id of the product.
I need to get all 4 of the cookies compare their value or name and display their properties in a small div in html.
I work in C# in Visual Studio and i am new in this.
Thank you in advance.

What I have tried:

This is my code for setting and getting a cookie Recent.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

/// <summary>
/// Summary description for Recent
/// </summary>
public class Recent
{
    //List<Product> basketList



    public static void AddCookie(string name,string nameid)
    {
        HttpCookie cookie = new HttpCookie(name);
        HttpContext.Current.Response.Cookies[name].Value = nameid;
        cookie.Value = HttpContext.Current.Response.Cookies[name].Value;
        cookie.Expires = DateTime.Now.AddDays(1);
        HttpContext.Current.Response.Cookies.Add(cookie);
    }



    public static string GetCookie(string name)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
        //List<Product> basketList = new List<Product>();
        if (cookie != null)
        {
            string objCartListString = cookie.Value.ToString();
            string[] objCartListStringSplit = objCartListString.Split('|');
            foreach (string s in objCartListStringSplit)
            {
                string[] ss = s.Split(',');
                if (ss[0] != "")
                {
                    Product model = new Product()
                    {
                        ProductID = Convert.ToInt32(ss[0]),

                    };
                    //basketList.Add(model);
                }

            }
        }
        return name;
    }






   
}





这是我在ProductDetails.cshtml中的代码(首先我设置名称和值,然后我用该名称检索cookie):



This is my code in ProductDetails.cshtml (First i set the name and the value and then i retreive the cookie with that name):

Recent.AddCookie(@Prod.Btitle.toGreeklish().ToString(),@Prod.ProductID.ToString());
    var co = Recent.GetCookie(@Prod.Btitle.toGreeklish().ToString());





这是html ul我需要显示产品的一些细节(Foreach循环不会遍历所有cookie但是它会遍历字符串本身,例如,如果strinng有7个字母,我得到7个实例li标签显示如下。):





This is the html ul where i need to display some of the details of the product (The Foreach loop does not go through all the cookie but instead it goes through the string itself, and for example if the strinng has 7 letters i get 7 instances of the li tag displayed below.):

@if(co != "" && co != null)
                       {
                           foreach (var cookie in co)
                           {
                       <li>
                           <div class="single-product">
                               <div class="product-img">
                                   <a href="#">
                                       <img id="img_c" class="primary-image" src="@prodImg" alt="" />
                                       <img class="secondary-image" src="/img/product/16.jpg" alt="" />
                                   </a>
                               </div>
                               <div class="product-content">
                                   <div class="pro-info">
                                       <h2 class="product-name" id="name_c"><a href="#">@co</a></h2>
                                       <div class="price-box">
                                           <span class="new-price" id="price_c">€@Prod.UnitCost.ToString("0.##")</span>
                                           <span class="old-price">£120.00</span>
                                       </div>
                                   </div>
                               </div>
                           </div>
                       </li>
                           }

                       }

推荐答案

不,你没有。

你必须在你的cookie中添加产品。



No, you don't.
You have to add products to your cookie.

HttpCookie MyCookie = new HttpCookie("YourCookieName");
MyCookie.Values["Product1"] = "Banana";
MyCookie.Values["Product2"] = "Orange";
MyCookie.Values["Product3"] = "Apple";
Response.Cookies.Add(MyCookie);





然后您可以通过访问您的产品值属性。注意, Value 属性以这种方式保持值: Key1 = Val1& Key2 = Val2& Key3 = Val3& Key4 = Val4& Key5 = Val5



详情请见:

HttpCookie Class(System.Web)| Microsoft Docs [ ^ ]

HttpCookie.Value Property(System.Web)| Microsoft Docs [ ^ ]

HttpCookie.Values Property(System.Web)| Microsoft Docs [ ^ ]







每当你在篮子里添加了一些你不需要创建新cookie的东西。您必须使用相同的cookie并为其添加另一个值。

您的 AddToBasket 方法可能如下所示:





Then you can access to your products via Values or Value property. Note, that Value property is keeping values this way: Key1=Val1&Key2=Val2&Key3=Val3&Key4=Val4&Key5=Val5

For further details, please see:
HttpCookie Class (System.Web) | Microsoft Docs[^]
HttpCookie.Value Property (System.Web) | Microsoft Docs[^]
HttpCookie.Values Property (System.Web) | Microsoft Docs[^]



Whenever you add something to the basket you don't need to create new cookie. You have to use the same cookie and add another value to it.
Your AddToBasket method may look like:

void AddToBasket(string sKey, string sValue)
{
    HttpCookie BasketCookie = null;
    if(Request.Cookies["BasketCookie"]!=null)
    {
        BasketCookie = (HttpCookie)Request.Cookies["BasketCookie"];
    }
    else
    {
        BasketCookie = new HttpCookie("BasketCookie");
    }
    BasketCookie.Values[sKey] = sValue;
    BasketCookie.Expires = DateTime.Now.AddHours(2)
    Response.Cookies.Add(BasketCookie);

}


这篇关于获取最新显示的所有Cookie列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆