我一直在尝试用代码清除
HttpContext.Response
.
最初,我用这个:
DateTime cookieExpires = DateTime.Now.AddDays(-1);
for (int i = 0; i < HttpContext.Request.Cookies.Count; i++)
{
HttpContext.Response.Cookies.Add(
new HttpCookie(HttpContext.Request.Cookies[i].Name, null) { Expires = cookieExpires });
}
但是,如果
OutOfMemoryException
因为
for
循环从不退出-每次将cookie添加到
Response
,它也会添加到“请求”中。
以下方法有效:
DateTime cookieExpires = DateTime.Now.AddDays(-1);
List<string> cookieNames = new List<string>();
for (int i = 0; i < HttpContext.Request.Cookies.Count; i++)
{
cookieNames.Add(HttpContext.Request.Cookies[i].Name);
}
foreach (string cookieName in cookieNames)
{
HttpContext.Response.Cookies.Add(
new HttpCookie(cookieName, null) { Expires = cookieExpires });
}
那么,到底是什么关系
HttpContext.Request.Cookies
和
HttpContext.Response.Cookies
?