无法将值附加到 cookie

Can't append value to cookie

我的 cookie 在第一个页面加载时填充了一个值。 但是,当我稍后尝试通过下面的代码用另一个值更新它,然后刷新页面时,它只显示初始值。 为什么我不能更新 cookie?

If Request.Cookies("lastviewed") Is Nothing Then
    Dim cookie As HttpCookie = New System.Web.HttpCookie("lastviewed", Request.RawUrl + "|" + photo + "|" + title + "|" + price)
    HttpContext.Current.Response.Cookies.Add(cookie)
Else
    lastviewed = Server.UrlDecode(Request.Cookies("lastviewed").Value)
    If Not lastviewed.Contains(Request.RawUrl + "|") Then
        If lastviewed.Split(";").Length < 5 Then
            If lastviewed.Split(";").Length > 0 Then
               lastviewed = lastviewed + ";" + Request.RawUrl + "|" + photo + "|" + title + "|" + price
            Else
                lastviewed = Request.RawUrl + "|" + photo + "|" + title + "|" + price
            End If
            'this part where I want to ADD a new value to the cookie does not seem to work
            Dim cookie As New System.Web.HttpCookie("lastviewed", lastviewed)
            HttpContext.Current.Response.Cookies.Add(cookie)
        End If
    End If
End If

因为cookie不能接受这些字符:

空格、DQUOTE、逗号、分号 和反斜杠。

所以,你可以改变分割符,比如@

If lastviewed.Split("@").Length < 5 Then
    If lastviewed.Split("@").Length > 0 Then
        lastviewed = lastviewed + "@" + Request.RawUrl + "|" + photo + "|" + Title + "|" + price

再试一次。