0

我注意到关于 asp.net 中多个 cookie 的一些事情,当您在不同的回发中添加新值时,较早的值将被删除,而最后的值将仅保留

- 例如 Default1.aspx.vb 中的 (VB.net)

Response.Cookies("mycookie")("abc") = 123

在 Default2.aspx.vb

Response.Cookies("mycookie")("def") = 456

现在,如果 default1 执行,“abc”值将存储在“mycookie”中,但如果然后 defualt2 执行,“abc”值将替换为新的“def”值!。

我尝试了以下解决方案,但我对此感到不舒服,因为我认为它占用了大量资源而没有意义:在 Default2.aspx.vb

Dim OldValues As NameValueCollection = Request.Cookies.Get("mycookie").Values
Response.Cookies.Get("mycookie").Values.Clear()
Response.Cookies.Get("mycookie").Values.Add(OldValues)
Response.Cookies.Get("mycookie").Values.Add("def", 456)

现在,是否有任何其他选项可以执行此操作,例如禁用设置中的某些内容或防止覆盖早期值的任何内容?

谢谢 :)

4

1 回答 1

0

嗯,没有想法?!

无论如何,由于没有其他选择,我决定使用我的代码并做一些封装,为了公共利益,我将我的代码发布在这里可能会使一些感兴趣的兄弟受益。

这里的主要思想是我开发一个大型 Web 应用程序,我需要在多层次范围内存储用户选择,例如按首选项保存订单、每页项目数等。以及多层次范围,例如长期保存首选项时间段(在 cookie 中)或在短时间内(在会话中)。

由于这些选择太多,我被迫使用多个 cookie,因为某些浏览器中允许的 cookie 数量有限。

这是执行此技巧的封装函数:

Public Enum StoredPropertyScope As SByte
    SessionAndCookies = 0
    SessionOnly = 1
    CookiesOnly = 2
End Enum
Public Shared Function GetStoredProperty(ByVal group As String, ByVal Key As String, Optional scope As StoredPropertyScope = StoredPropertyScope.SessionAndCookies) As Object
    If (scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.SessionOnly) And Not (HttpContext.Current.Session(group & "-" & Key) Is Nothing) Then
        Return HttpContext.Current.Session(group & "-" & Key)
    Else
        If (scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.CookiesOnly) And Not (Request.Cookies(group) Is Nothing) Then
            Return Request.Cookies(group)(Key)
        Else
            Return Nothing
        End If
    End If
End Function
Public Shared Sub SetStoredProperty(ByVal group As String, ByVal key As String, value As String, Optional scope As StoredPropertyScope = StoredPropertyScope.SessionAndCookies)
    If scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.CookiesOnly Then
        If Request.Cookies(group) Is Nothing Then
            Response.Cookies(group)(key) = value
            Response.Cookies(group).Expires = Now.AddYears(1)
        Else
            Dim OldValues As NameValueCollection = Request.Cookies.Get(group).Values
            OldValues.Item(key) = value
            Response.Cookies.Get(group).Values.Clear()
            Response.Cookies.Get(group).Values.Add(OldValues)
            Response.Cookies(group).Expires = Now.AddYears(1)
        End If
    End If
    If scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.SessionOnly Then
        HttpContext.Current.Session(group & "-" & key) = value
    End If
End Sub
Public Overloads Shared Sub RemoveStoredProperty(ByVal group As String, ByVal key As String, Optional scope As StoredPropertyScope = StoredPropertyScope.SessionAndCookies)
    If scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.CookiesOnly Then
        If Not (Request.Cookies(group) Is Nothing) AndAlso Request.Cookies(group).HasKeys Then
            Dim OldValues As NameValueCollection = Request.Cookies.Get(group).Values
            OldValues.Remove(key)
            Response.Cookies.Get(group).Values.Clear()
            Response.Cookies.Get(group).Values.Add(OldValues)
            Response.Cookies(group).Expires = Now.AddYears(1)
        End If
    End If
    If scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.SessionOnly Then
        HttpContext.Current.Session.Remove(group & "-" & key)
    End If
End Sub
Public Overloads Shared Sub RemoveStoredProperty(ByVal group As String)
    'remove all from cookies only
    Response.Cookies(group).Expires = DateTime.Now.AddDays(-1)
End Sub
Public Shared Function CheckStoredProperty(ByVal group As String, ByVal key As String) As StoredPropertyScope
    'return where is property stored or -1 if not exists
    Dim result As StoredPropertyScope = -1
    If (Not Request.Cookies(group) Is Nothing) AndAlso (Not Request.Cookies(group)(key) Is Nothing) Then
        result = StoredPropertyScope.CookiesOnly
    End If
    If Not HttpContext.Current.Session(group & "-" & key) Is Nothing Then
        If result = -1 Then result = StoredPropertyScope.SessionOnly Else result = StoredPropertyScope.SessionAndCookies
    End If
    Return result
End Function

如何使用 1- 首先,您需要删除属性组和子键的所有名称,因为您会将其用作字符串值,因此您有责任确保其正确。

2-像这样存储价值使用代码:

SetStoredProperty("userSelections", "itemsOrderedBy", "Price", StoredPropertyScope.SessionAndCookies)
  • 第一个参数是组名,它将是cookie名称,第二个参数是proberty名称(cookie中的子值),第二个参数是problerty的值,第四个参数是可选的,用于确定在哪里您想要存储此值(仅会话或仅 cookie 或两者)。

*注意(cookie 有效期为 1 年,您可以直接从代码中更改)

3-检索值:

GetStoredProperty("userSelections", "itemsOrderdBy",  StoredPropertyScope.CookiesOnly)
  • 第三个参数是可选的,如果您选择 SessionAndCookies,则返回值的优先级将是会话

4-删除价值:

RemoveStoredProperty("userSelections")  
RemoveStoredProperty("userSelections", "itemsOrderdBy", StoredPropertyScope.SessionAndCookies) 
  • 第一行将删除整个组(cookie),它仅用于 cookie。第二行将从可选的第三个参数中确定的范围中删除指定的属性

5- 财产存放地点:

 CheckStoredProperty("userSelections", "itemsOrderdBy")
  • 此函数将搜索探测器,如果存在将返回它在哪里(StoredPropertyScope 枚举),如果不存在则返回 -1

我希望这会很有用,如果有更好的解决方案,请告诉我。谢谢

于 2012-11-10T02:35:15.487 回答