嗯,没有想法?!
无论如何,由于没有其他选择,我决定使用我的代码并做一些封装,为了公共利益,我将我的代码发布在这里可能会使一些感兴趣的兄弟受益。
这里的主要思想是我开发一个大型 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
我希望这会很有用,如果有更好的解决方案,请告诉我。谢谢