今天早上我无意中看到了下面的代码片段,我很惊讶,因为它运行得很好。
请不要看它的逻辑,我只是好奇为什么 HttpCookieCollection(在这种情况下为 Request.Cookies)在 foreach 循环中返回一个字符串(cookie 名称)而不是一个 HttpCookie 对象。这是一个一致性问题,因为我们通常通过索引/名称获取此集合中的 HttpCookie 对象吗?
谢谢,
foreach (string cookieKey in System.Web.HttpContext.Current.Request.Cookies)
{
HttpCookie tmpCookie = System.Web.HttpContext.Current.Request.Cookies[cookieKey];
if (tmpCookie != null && tmpCookie["RecentlyVisited"] != null)
{
cookie.Add(tmpCookie);
}
}