在asp.net中使用cookie mvc c#

tit*_*iti 31 asp.net cookies asp.net-mvc-2

我想使用cookie在我的网站上注册几页的参数.我尝试了下面的代码,但不喜欢我想要的:

 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();
 }
Run Code Online (Sandbox Code Playgroud)

我在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());
}
%>
Run Code Online (Sandbox Code Playgroud)

问题:当我单击另一个Request.QueryString["dep"]为null的页面时,我显示的cookie为null.

如果我们尚未清除cookie,如何将其存储在cookie中而不会丢失?

Yan*_*nis 63

我不确定我是否理解这是关于如何正确地向客户端发送cookie或者使用查询字符串params的一些错误的问题.所以我会发布正确的发送cookie的方式,如果我误解了,请随时纠正我.

无论如何,我相信这一点:

HttpCookie cookie = new HttpCookie("search");
Run Code Online (Sandbox Code Playgroud)

将重置搜索cookie

获取cookie:

HttpCookie cookie = HttpContext.Request.Cookies.Get("some_cookie_name");
Run Code Online (Sandbox Code Playgroud)

检查cookie的存在:

HttpContext.Request.Cookies["some_cookie_name"] != null
Run Code Online (Sandbox Code Playgroud)

要保存cookie:

HttpCookie cookie = new HttpCookie("some_cookie_name");
HttpContext.Response.Cookies.Remove("some_cookie_name");
HttpContext.Response.SetCookie(cookie );
Run Code Online (Sandbox Code Playgroud)

  • 为了澄清,你将这个代码放在MVC项目中哪里以避免用垃圾填满你的控制器? (5认同)

Shi*_*abh 18

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

您只需将此类放入代码中即可.

在这里,我把我的类与静态方法

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

使用这些,您可以轻松地在cookie中存储值,并在需要时获取值

使用这些方法很简单

设置Cookie:

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

对于获取Cookie:

string currency= CookieStore.GetCookie("currency");
Run Code Online (Sandbox Code Playgroud)