How to retrieve content from a cookie

0

How do I retrieve content from a cookie, this cookie has the material ID that will be requested and the amount, which I need to retrieve to insert into the database. I've looked a lot on the internet and have not figured it out yet. It follows the code of how I create the cookie and I enter the values in it.

    [Authorize]
    public void AddCarrinho(String Qtd, String Id)
    {
        HttpCookie cookie;
        if(Request.Cookies["pedido"] == null)
        {
            cookie = new HttpCookie("pedido");
        }
        else
        {
            cookie = (HttpCookie)Request.Cookies["pedido"];
        }
        cookie.Expires = DateTime.Now.AddHours(1); //cookie expira depois de 1 hora
        cookie.Values.Add(Id, Qtd);
        Response.Cookies.Add(cookie);
    }

Edited

Follow the code as I did after Gypsy Morison's response and it did not work.

    public void AddCarrinho(String Id, String Qtd)
    {
        HttpCookie cookie;
        if (Request.Cookies["teste"] == null)
        {
            cookie = new HttpCookie("teste");
        }
        else
        {
            cookie = (HttpCookie)Request.Cookies["teste"];
        }
        cookie.Expires = DateTime.Now.AddHours(1); //cookie expira depois de 1 hora
        cookie.Values.Add(Id, Qtd);
        Response.Cookies.Add(cookie);
    }

    public ActionResult Recuperar()
    {
        Teste t = new Teste();

        List<Teste> lst = new List<Teste>();

        if(Request.Cookies["teste"]["Id"] != null && Request.Cookies["teste"]["Qtd"] != null)
        {
            for(int i = 0; i < Request.Cookies.Count; i++)
            {
                t.ID = Request.Cookies["teste"]["Id"];
                t.Quantidade = Request.Cookies["teste"]["Qtd"];
                lst.Add(t);
            }
        }

        return View(lst);
    }

Edited

    
asked by anonymous 30.09.2014 / 19:26

1 answer

2

A Cookie is a dictionary, logo:

if (Request.Cookies["pedido"][Id] != null) 
{ 
    minhaInformacao = Request.Cookies["pedido"][Id]; 
}
    
30.09.2014 / 19:29