title: Using cookie in asp.net mvc c#
body: <p>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 :</p>
  
  <pre><code> 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();
   }
  </code></pre>
  
  <p>I read it in site.master :</p>
  
  <pre><code>&lt;% 
  
  HttpCookie cookie = Request.Cookies["search"] ;
  
  if ((cookie != null) &amp;&amp; (cookie.Value != ""))
  {
      Response.Write(cookie.Values["dep_name"].ToString() + "---" +   
      cookie.Values["cat_name"].ToString() + "---" + cookie.Values["brand"].ToString());
  }
  %&gt;
  </code></pre>
  
  <p>Problem: When I click to another page that <code>Request.QueryString["dep"]</code> is null, the cookie that I display is null to. </p>
  
  <p>How to store it in the cookie without losing while we not yet clear the cookie?</p>