2012-02-06 49 views
24

我想使用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?

回答

56

我不知道我明白,如果这是一个关于如何正确地向客户端发送cookie或与查询字符串的一些错误的问题。所以我会张贴正确的方式发送cookie,并随时纠正我,如果我误解了。

在任何情况下,虽然,我相信这一点:

HttpCookie cookie = new HttpCookie("search"); 

将重置搜索饼干

为了得到一个cookie:

HttpCookie cookie = HttpContext.Request.Cookies.Get("some_cookie_name"); 

要检查一个cookie的存在:

HttpContext.Request.Cookies["some_cookie_name"] != null 

要保存一个cookie:

HttpCookie cookie = new HttpCookie("some_cookie_name"); 
HttpContext.Response.Cookies.Remove("some_cookie_name"); 
HttpContext.Response.SetCookie(cookie); 
+0

我一直在做网页的东西了将近六年,我得到了包括设置在第一次一个cookie只是最近的任务。奇怪,狂野的东西。这有帮助,谢谢! – MrBoJangles 2014-05-14 19:40:36

+4

为了澄清,你将这个代码放在MVC项目的哪个位置,以避免用垃圾填满你的控制器? – 2015-05-13 15:14:31

+0

没有行'HttpContext.Response.Cookies.Remove'删除cookie不保存吗? – Jaylen 2017-03-23 20:31:07

14

我已经组织了cookie获取并以有组织的方式插入,以便它可以在整个应用程序中使用。为此我提出了两种方法SetCookieGetCookie

你可以简单地把这个类放在你的代码中,然后解决。

在这里,我把我的类使用这些,只要使用这些方法需要

,你可以很容易地存储在cookie值和获取价值的静态方法

public class CookieStore 
{ 
    public static void SetCookie(string key, string value, TimeSpan expires) 
    { 
     HttpCookie encodedCookie = HttpSecureCookie.Encode(new HttpCookie(key, value)); 

     if (HttpContext.Current.Request.Cookies[key] != null) 
     { 
      var cookieOld = HttpContext.Current.Request.Cookies[key]; 
      cookieOld.Expires = DateTime.Now.Add(expires); 
      cookieOld.Value = encodedCookie.Value; 
      HttpContext.Current.Response.Cookies.Add(cookieOld); 
     } 
     else 
     { 
      encodedCookie.Expires = DateTime.Now.Add(expires); 
      HttpContext.Current.Response.Cookies.Add(encodedCookie); 
     } 
    } 
    public static string GetCookie(string key) 
    { 
     string value = string.Empty; 
     HttpCookie cookie = HttpContext.Current.Request.Cookies[key]; 

     if (cookie != null) 
     { 
      // For security purpose, we need to encrypt the value. 
      HttpCookie decodedCookie = HttpSecureCookie.Decode(cookie); 
      value = decodedCookie.Value; 
     } 
     return value; 
    } 

} 

一样简单用于设置Cookie:

CookieStore.SetCookie("currency", "GBP", TimeSpan.FromDays(1)); // here 1 is no of days for cookie to live 

对于获取的Cookie:

string currency= CookieStore.GetCookie("currency"); 
+0

保存时,为什么更新现有的cookie而不是设置新的cookie? – 2014-01-31 18:30:25

+1

我得到了一个问题,我发现cookie的两次,当看到有效期限为1月1日,所以想要更新它。 – 2014-01-31 18:33:44

+0

您是否使用http://www.nuget.org/packages/httpsecurecookie/中的HttpSecureCookie? – 2014-01-31 18:37:57