I want to register the parameter of a few pages in my web site using cookie. I tried the below code but not like what I want :
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();
}
I read it in site.master :
<%
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());
}
%>
Problem: When I click to another page that Request.QueryString["dep"]
is null, the cookie that I display is null to.
How to store it in the cookie without losing while we not yet clear the cookie?