Home >Web Front-end >HTML Tutorial >aspx page state management cookie and 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 Get 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, delete cookies
//将cookie有效期设置为过去某个时间,浏览器会检查自动删除 HttpCookie cookie = Request.Cookies["user"]; cookie.Expires = DateTime.Now.AddHours(-1);
cookie.Domain="baiyun.com";cookie.Domain="sell.baiyun.com";
so that the cookie can be used for the main domain and subdomains