Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
    }






   
}


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());


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>
                           }

                       }
Posted
Updated 26-Feb-19 20:48pm

1 solution

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

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


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[^]


[EDIT]
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:

C#
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);

}
 
Share this answer
 
v4
Comments
simple world 27-Feb-19 8:37am    
@MaciejLos Hello Thank you for responding.
I did what you said but still i get multiple cookies and each cookie then it get as value the name=value.
And i dont know how to retrieve all the cookies that exist in the browser into a list so that i can loop through and be able to display them.
Maciej Los 27-Feb-19 8:57am    
If you'd do what i said, you'll get only one cookie...
For further details, please see: How to create and Retrieve the cookies in C# and ASP.net[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900