【发布时间】:2009-07-02 22:43:37
【问题描述】:
所以,我有一系列产品页面,我想做的只是将最近查看的 5 个产品存储在 cookie 中,以便可以将其显示为网站历史记录。我遇到的问题不是将五个初始项目添加到 cookie 中,而是当他们查看 6、7 或 10 个项目时。有没有人对如何解决这个问题有任何真正体面的建议?
目前我有这个有缺陷的逻辑(为简洁起见,我已经替换了 cookie 名称 (xxx));
Dim i As Integer = 0
Dim productcount As Integer = 0
If HttpContext.Current.Request.Cookies("xxx") Is Nothing Then
Dim gingernuts As New HttpCookie("xxx")
gingernuts.Values("productcount") = 0
gingernuts.Expires = DateTime.Now.AddDays(365)
HttpContext.Current.Response.Cookies.Add(gingernuts)
End If
productcount = HttpContext.Current.Request.Cookies("xxx")("productcount")
For i = 0 To productcount
If HttpContext.Current.Request.Cookies("xxx")("product" & i & "") = "" Then
HttpContext.Current.Response.Cookies("xxx")("product" & i & "") = Request.QueryString("id")
Else
HttpContext.Current.Response.Cookies("xxx")("product" & i & "") = HttpContext.Current.Request.Cookies("xxx")("product" & i & "")
End If
Next
If productcount = 5 Then
HttpContext.Current.Response.Cookies("xxx")("productcount") = 5
HttpContext.Current.Response.Cookies("xxx")("product0") = ""
Else
HttpContext.Current.Response.Cookies("xxx")("productcount") = productcount + 1
End If
欢迎和赞赏建议和批评。
克里斯
【问题讨论】: