且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在asp.net mvc的C#中使用的cookie

更新时间:2023-02-14 20:11:42

我不敢肯定我的理解,如果这是一个关于如何正确发送cookie到客户端或一些bug与你的查询字符串PARAMS问题。所以,我会邮寄cookie的正确方法,并随时纠正我,如果我误解了。

I m not sure I understand if this is a question about how to properly send cookies to the client or some bug with your querystring params. So I ll post the proper way of sending cookies and feel free to correct me if I misunderstood.

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

In any case though, I believe this:

HttpCookie cookie = new HttpCookie("search");

将重置搜索的cookie

will reset the search cookie

要得到一个cookie:

To get a cookie:

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

要检查一个cookie的存在:

To check for a cookie's existence:

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

要保存一个cookie:

To save a cookie:

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