asp.net(c#)使用中發(fā)現(xiàn)用Request.Cookies.Remove()無(wú)法刪除cookie,最后發(fā)現(xiàn)可通過(guò)將過(guò)期時(shí)間設(shè)置成過(guò)去的時(shí)間來(lái)完成刪除。代碼如下:
HttpCookie hc = Request.Cookies["Value"];
hc.Expires = DateTime.Now.AddDays(-1);
Response.AppendCookie(hc); //這句一定要加上,否則無(wú)法刪除
順便整理下關(guān)于Cookie的操作:
一 創(chuàng)建
1 單值的創(chuàng)建
HttpCookie hc = new HttpCookie("Value");
hc.Value = "value";
Response.AppendCookie(hc);
2 多值的創(chuàng)建
HttpCookie hc = new HttpCookie("Value");
hc["Value1"] = "value1";
hc["Value2"] = "value2;
Response.AppendCookie(hc);
二 讀取
1 單值的讀取
string value = Request.Cookies["Value"].Value;
2 多值的讀取
string value1 = Request.Cookies["Value"]["Value1"].ToString();
string value2 = Request.Cookies["Value"]["Value2"].ToString();