ホームページ >ウェブフロントエンド >htmlチュートリアル >aspx ページ状態管理 Cookie と ViewState_html/css_WEB-ITnose
protected void Button2_Click(object sender, EventArgs e) { HttpCookie cookie = new HttpCookie("user"); cookie.Value = "这是cookie"; cookie["sex"] = "女"; cookie.Values.Add("age", "18"); cookie.Expires = DateTime.Now.AddHours(1); Response.AppendCookie(cookie);//将Cookie追加到内部cookie //Response.Cookies.Add(cookie);//跟上面一样 }
2获取Cookie
protected void Button1_Click(object sender, EventArgs e) { //获取客户端发送的Cookie HttpCookie cookie1 = Request.Cookies["user"]; if (cookie1 == null) { Response.Write("木有发现cookie"); } else { Response.Write("cooki值为:"+cookie1.Value+"<br/>"); Response.Write("sex值为:"+cookie1["sex"]+"<br/>"); Response.Write("age值为:"+cookie1["age"]+"<br/>"); } }
3,删除cookie
//将cookie有效期设置为过去某个时间,浏览器会检查自动删除 HttpCookie cookie = Request.Cookies["user"]; cookie.Expires = DateTime.Now.AddHours(-1);
cookie.Domain="baiyun.com";cookie.Domain="sell.baiyun.com";
这样cookie就能用于主域和子域了