asp.net - 最近访问的五页 Cookie

标签 asp.net vb.net cookies

所以,我有一系列产品页面,我想要做的就是将最近查看的 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

欢迎和赞赏建议和批评。

克里斯

最佳答案

只需使用一个简单的 cookie 值,即最近查看的产品 ID 的逗号分隔列表。 (注意我的 VB.NET 是那么强大)。

MostRecentIDs as String() '' // Instance level variables
Const ListSize = 5

'' // in a method somewhere
context As HttpContext = HttpContext.Current
cookie As HttpCookie = context.Request.Cookies("mri")
mri As Queue(Of String)

If cookie Is Nothing Then
    mri = New Queue(Of String)(cookie.Value.Split(",".ToCharArray())
Else
    mri = New Queue(Of String)
    cookie = New HttpCookie("mri")
End If

If mri.Contains(Request.QueryString("id")) Then

    If mri.Count >= ListSize Then mri.Dequeue()

    mri.Enqueue(Request.QueryString("id"))

End If

MostRecentIDs = mri.ToArray();

cookie.Value = String.Join(",", MostRecentIDs)
cookie.Expires = DateTime.Now.AddDays(365)

context.Response.Add(cookie)
  • 现在,您可以以简单的字符串数组的形式访问最新的产品 ID。
  • 此代码处理尚未创建 cookie 的情况。
  • cookie 本身要小得多。
  • 管理列表的大小可以很容易地参数化。

  • 上面的代码基于下面经过测试的 C# 代码,我将其放入一个空的 ASPX 文件中:-
    const int listSize = 8;
    string[] _mru;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["mru"];
        Queue<string> mru;
        if (cookie != null)
        {
            mru = new Queue<string>(cookie.Value.Split(','));
        }
        else
        {
            mru = new Queue<string>();
            cookie = new HttpCookie("mru"); 
        }
    
        if (!mru.Contains(Request.QueryString["id"]))
        {
            if (mru.Count >= listSize) mru.Dequeue();
    
            mru.Enqueue(Request.QueryString["id"]);
        }
    
        _mru = mru.ToArray();
    
        cookie.Value = String.Join(",", _mru);
        cookie.Expires = DateTime.Now.AddDays(365);
    
        Response.Cookies.Add(cookie);
    
    }
    

    关于asp.net - 最近访问的五页 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1068523/

    相关文章:

    c# - 如何使信号器客户端避免表单例份验证?

    javascript - 将页面从 Iframe 更改为主窗口

    c# - 上传图片 - 外键冲突

    c# - ASP.NET - App_Code - 在同一文件夹中混合 VB 和 C#

    vb.net - vb.net 中的 List.ForEach - 让我困惑

    javascript - 难以通过 JavaScript 中的获取请求发送 cookie

    javascript - WebMethod 无法在 ASP.Net Web 角色(Web 表单)中工作?

    .net - 使用 SqlBulkCopy 在不同来源之间传输数据

    http - 浏览器如何处理没有路径和域的cookie

    python - Instabot KeyError : 'urlgen'