我想使用cookie在我的网站上注册几个页面的参数。我想下面的代码,但不喜欢什么,我想:在asp.net mvc c中使用cookie#
<%
HttpCookie cookie = Request.Cookies["search"] ;
if ((cookie != null) && (cookie.Value != ""))
{
Response.Write(cookie.Values["dep_name"].ToString() + "---" +
cookie.Values["cat_name"].ToString() + "---" + cookie.Values["brand"].ToString());
}
%>
问题:
public ActionResult Index(int? dep, int? cat)
{
......
string theDept = Request.QueryString["dep"];
HttpCookie cookie = new HttpCookie("search");
cookie.Values["dep_name"] = theDept;
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
return View();
}
我的Site.Master读它当我点击到另一个页面,Request.QueryString["dep"]
为null,该cookie是我显示为null。
如何将它存储在cookie中而不会丢失,而我们尚未清除cookie?
我一直在做网页的东西了将近六年,我得到了包括设置在第一次一个cookie只是最近的任务。奇怪,狂野的东西。这有帮助,谢谢! – MrBoJangles 2014-05-14 19:40:36
为了澄清,你将这个代码放在MVC项目的哪个位置,以避免用垃圾填满你的控制器? – 2015-05-13 15:14:31
没有行'HttpContext.Response.Cookies.Remove'删除cookie不保存吗? – Jaylen 2017-03-23 20:31:07