做网站免不了要保存用户状态,ASP.NET MVC 框架的Controller提供了对Cookie的操作
代码如下:
HttpCookie cookies = new HttpCookie("UserName", user.UserName); //设置为永久Cookie 去掉则为临时cookie 用户关闭后自动清除,类似session cookies.Expires = DateTime.Parse("03/24/2009"); this.Response.SetCookie(cookies); RenderView("UserPanel", user);
页面Page_onload
Response.Write("Cookie:" + Request.Cookies["UserName"].Value);