前五页访问Cookie

时间:2009-07-01 11:06:33

标签: asp.net vb.net cookies

所以,我有一系列产品页面,我想要做的就是存储在cookie中查看的最后5个产品,这样它就可以显示为网站历史记录。我遇到的问题是没有将五个初始项添加到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

建议和批评受到欢迎和赞赏。

克里斯

3 个答案:

答案 0 :(得分:4)

只需使用一个简单的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);

}

答案 1 :(得分:1)

如果我理解你的问题,我就会这样做:) 如果产品数量少于5,则只需添加新产品。否则将product0替换为product1至4,然后在4

添加新产品
    If productcount < 5 Then 'Do the null value check before this
        HttpContext.Current.Response.Cookies("xxx")("productcount") = productCount + 1
        HttpContext.Current.Response.Cookies("xxx")("product" & productCount + 1) = ""

    Else
        For i = 0 To productcount - 1
           'Replace product 0 with 1, 1 with 2...till 3 with 4 
            HttpContext.Current.Response.Cookies("xxx")("product" & i & "") = HttpContext.Current.Response.Cookies("xxx")("product" & i + 1& "")

       Next
       HttpContext.Current.Response.Cookies("xxx")("product" & 4 & "") =  Request.QueryString("id")
    End If

答案 2 :(得分:0)

Anthony完美地回答了这个问题,但是为了将来参考和使用VB的人,这是代码:

    Imports System.Collections.Generic

    Const listSize As Integer = 5
    Private _mru As String()

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cookie As HttpCookie = Request.Cookies("mru")

        Dim mru As Queue(Of String)
        If cookie IsNot Nothing Then
            mru = New Queue(Of String)(cookie.Value.Split(","c))
        Else
            mru = New Queue(Of String)()
            cookie = New HttpCookie("mru")
        End If

        If mru.Count >= listSize Then
            mru.Dequeue()
        End If

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

        _mru = mru.ToArray()

        cookie.Value = [String].Join(",", _mru)
        cookie.Expires = DateTime.Now.AddDays(365)


        Response.Cookies.Add(cookie)
End Sub

非常感谢这里的所有贡献者。