当使用Request.Cookies时过Response.Cookies? [英] When to use Request.Cookies over Response.Cookies?

查看:181
本文介绍了当使用Request.Cookies时过Response.Cookies?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难道我用的响应时,在页面的事件(例如负载),因为这是从ASP.NET的响应,并请求时pressing一个按钮,因为这是一个反应将ASP.NET处理?还是有更多的吗?

Do I use response when at a page event (e.g. load) as this is a response from ASP.NET, and request when pressing a button as this is a response going to ASP.NET for processing? Or is there more to it?

推荐答案

它们是两种不同的东西,一个的保存 [回应],其他的 [请求]

They are 2 different things, one SAVES [Response], the other READS [Request]

在一个Cookie(信息学说):)
您保存一个小文件的一段包含类型的对象时间 字符串

in a Cookie (informatics speaking) :) you save a small file for a period of time that contains an object of the type string

在.NET框架您保存一个cookie 做:

HttpCookie myCookie = new HttpCookie("MyTestCookie");
DateTime now = DateTime.Now;

// Set the cookie value.
myCookie.Value = now.ToString();
// Set the cookie expiration date.
myCookie.Expires = now.AddMinutes(1);

// Add the cookie.
Response.Cookies.Add(myCookie);

Response.Write("<p> The cookie has been written.");

您写一个Cookie,将可一分钟...通常我们做的 now.AddMonth(1)的,所以你可以保存一个cookie一整个月。

You wrote a cookie that will be available for one minute... normally we do now.AddMonth(1) so you can save a cookie for one entire month.

取回cookie的,则使用Request(你是请求),如:

To retrieve a cookie, you use the Request (you are Requesting), like:

HttpCookie myCookie = new HttpCookie("MyTestCookie");
myCookie = Request.Cookies["MyTestCookie"];

// Read the cookie information and display it.
if (myCookie != null)
   Response.Write("<p>"+ myCookie.Name + "<p>"+ myCookie.Value);
else
   Response.Write("not found");

记住:

要删除一个Cookie,没有直接code,关键是为保存相同Cookie名称与已经通过,例如到期日期, now.AddMinutes (-1)

To Delete a Cookie, there is no direct code, the trick is to Save the same Cookie Name with an Expiration date that already passed, for example, now.AddMinutes(-1)

这将删除cookie。

this will delete the cookie.

正如你可以看到,每一个cookie的生命时间到期时,该文件从系统自动删除的时间。

As you can see, every time that the time of life of the cookie expires, that file is deleted from the system automatically.

这篇关于当使用Request.Cookies时过Response.Cookies?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆