将数据存储在HttpRequest对象中以保持页面加载的生命期

问题描述 投票:0回答:1

我正在尝试是否有可能在HttpRequest中存储一个对象,该对象可以在整个加载页面的生命周期中获取。

在类别网格上,我正在显示许多不同的产品。每个产品都可以添加到愿望清单。我对WishList有一个自定义控件,该控件生成用户当前WishList的下拉列表。如果我有20种产品,则此控件将被命中20次,并尝试多次从我的数据层加载对象。我一直在使用Session并尝试过HttpContext.Cache,但是据我了解,这些将持续X分钟。

因此,我想知道是否有一种方法可以使用HttpRequest对象存储我的List(Of WishList),那样,每当我击中自定义控件时,我都可以抓住它。

谢谢

编辑:要添加一些代码,这就是我想在自定义控件中设置我的媒体资源的方式(或为了获得更好的建议)

Public Property WishLists As Collection(Of Catalog.WishList)
        Get
            If _wishLists Is Nothing Then

                If Me.Page.Request.SomeMethod("MyCustomObject") IsNot Nothing Then
                    _wishLists = TryCast(Me.Page.Request.SomeMethod("MyCustomObject"), List(Of Catalog.WishList))
                Else
                    _wishLists = Catalog.WishList.FindByUserId(SessionManager.GetCurrentUserId())
                    Me.Page.Request.SomeMethod("MyCustomObject") = _wishLists
                End If

            End If
            Return _wishLists
        End Get
        Set(value As Collection(Of Catalog.WishList))
            _wishLists = value
        End Set
    End Property
asp.net vb.net memory-management webforms httprequest
1个回答
0
投票
将此代码放置为可从项目访问(请检查我的其他asp.net回答如何,并使用此方法来管理整个用户任务。

基于会话的asp.net id我将创建该特定会话中每个用户的对象集合。如果用户在您的网站页面左右移动,则这些变量的值不会改变。

如果您的用户从您的站点返回,则需要使用另一种方法(客户端,例如cookie或IndexedDB)>代码:

Private ObjsParams As Dictionary(Of String, Object) Private ObjsVariables As Dictionary(Of String, Object) Friend Sub Init() If AspNetId IsNot Nothing Then If ObjsParams Is Nothing Then ObjsParams = New Dictionary(Of String, Object) If ObjsVariables Is Nothing Then ObjsVariables = New Dictionary(Of String, Object) End If End Sub Function GetAppContext() As HttpContextBase If HttpContext.Current IsNot Nothing AndAlso HttpContext.Current.Request IsNot Nothing AndAlso HttpContext.Current.Request.RequestContext IsNot Nothing AndAlso HttpContext.Current.Request.RequestContext.HttpContext IsNot Nothing Then Return HttpContext.Current.Request.RequestContext.HttpContext End If If HttpContext.Current IsNot Nothing Then Return New HttpContextWrapper(HttpContext.Current) End If Return Nothing End Function Friend ReadOnly Property AspNetId As String Get Dim CtxBase As HttpContextBase = GetAppContext().Request.RequestContext.HttpContext Try Dim getSessionId As Func(Of String) = Function() Dim mSessionID As String = "" If CtxBase IsNot Nothing AndAlso CtxBase.Request IsNot Nothing AndAlso CtxBase.Request.Cookies IsNot Nothing AndAlso CtxBase.Request.Cookies.Count > 0 AndAlso CtxBase.Request.Cookies.Item("ASP.NET_SessionId") IsNot Nothing Then mSessionID = CtxBase.Request.Cookies.Item("ASP.NET_SessionId").Value End If If Len(mSessionID) = 0 Then If CtxBase IsNot Nothing AndAlso CtxBase.Session IsNot Nothing Then If CtxBase.Session("SessionID") IsNot Nothing Then mSessionID = CStr(CtxBase.Session("SessionID")) End If End If End If If Len(mSessionID) = 0 Then If CtxBase IsNot Nothing AndAlso CtxBase.Session IsNot Nothing Then If CtxBase.Session.SessionID IsNot Nothing Then mSessionID = CtxBase.Session.SessionID End If End If End If If Len(mSessionID) = 0 Then If CtxBase IsNot Nothing AndAlso CtxBase.Request IsNot Nothing AndAlso CtxBase.Request.Params IsNot Nothing AndAlso CtxBase.Request.Params.HasKeys Then mSessionID = CtxBase.Request.Params("ASP.NET_SessionId") End If End If Return mSessionID End Function Return getSessionId() Catch ex As Exception ' Here is comming if you use async methods If CtxBase IsNot Nothing AndAlso CtxBase.Request IsNot Nothing AndAlso CtxBase.Request.Params IsNot Nothing AndAlso CtxBase.Request.Params.HasKeys Then Dim CERT_COOKIE As String = CtxBase.Request.Params("HTTP_COOKIE") If Len(CERT_COOKIE) > 0 Then Return CERT_COOKIE End If End If ' desperate Return Nothing End Try End Get End Property Friend Function GetObj(chiave As String) As Object Dim res As Object = Nothing If ObjsVariables IsNot Nothing AndAlso AspNetId IsNot Nothing Then If ObjsVariables.ContainsKey(AspNetId & "~" & chiave) Then res = ObjsVariables(AspNetId & "~" & chiave) End If End If Return res End Function Friend Sub SetObj(chiave As String, valore As Object) If ObjsVariables IsNot Nothing AndAlso AspNetId IsNot Nothing Then If Not ObjsVariables.ContainsKey(AspNetId & "~" & chiave) Then ObjsVariables.Add(AspNetId & "~" & chiave, valore) Else ObjsVariables(AspNetId & "~" & chiave) = valore End If End If End Sub Function GetParams(chiave As String) As Object Dim res As Object = Nothing Try If ObjsParams IsNot Nothing AndAlso AspNetId IsNot Nothing Then If ObjsParams.ContainsKey(AspNetId & "~" & chiave) Then res = ObjsParams(AspNetId & "~" & chiave) End If End If Catch ex As Exception 'Log here End Try Return res End Function Sub SetParams(chiave As String, valore As Object) Try If ObjsParams IsNot Nothing AndAlso AspNetId IsNot Nothing Then If Not ObjsParams.ContainsKey(AspNetId & "~" & chiave) Then ObjsParams.Add(AspNetId & "~" & chiave, valore) Else ObjsParams(AspNetId & "~" & chiave) = valore End If End If Catch ex As Exception 'Log here End Try End Sub

用法:创建一个全局asax文件,并在会话开始事件中初始化会话变量

Public Class Global_asax
    Inherits System.Web.HttpApplication

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Try
              Init()
        Catch ex As Exception
            ‘Log here
        End Try
    End Sub

End Class

然后通过字符串标识创建属性的无穷大

  ‘Your wishList
    Public Property WishList As List(Of Catalog.WishList)
        Get
            Return CType(GetParams("WishList"), List(Of Catalog.WishList))
        End Get
        Set(value As List(Of Catalog.WishList))
            SetParams("WishList", value)
        End Set
    End Property


    ‘Other property 
    Public Property OtherProperty As String
        Get
            Return CType(GetParams("OtherProperty"), String)
        End Get
        Set(value As String)
            SetParams("OtherProperty", value)
        End Set
    End Property

然后您的用户进入您的网站,WishList保留其价值添加:

WishList.Add(New Catalog.WishList From {"Alfa ", "Beta", "Zeta"})

正在获取:

 For Each mCat In WishList
        'Print WishList here
 Next
© www.soinside.com 2019 - 2024. All rights reserved.