【问题标题】:Session variable removal question. as related to a paged data source会话变量删除问题。与分页数据源相关
【发布时间】:2009-12-21 18:12:11
【问题描述】:

我继承了一些非常古老的 ASP.Net 代码。最初在 1.0 中编写,然后转换为 2.0 有一个使用自定义寻呼机控件的页面。该控件具有以下逻辑:

Private _DataSource As PagedDataSource
Public Property DataSource() As PagedDataSource
    Get
        If Not IsNothing(Session("DataSource")) Then
            _DataSource = Session("DataSource")
            Return _DataSource
        End If
        Return Nothing
    End Get
    Set(ByVal value As PagedDataSource)
        _DataSource = value
        If Not IsNothing(value) Then
            Session("DataSource") = value
        Else
            Session("DataSource") = Nothing
        End If
    End Set
End Property

使用该寻呼机的页面中包含以下逻辑:

Private PagedData As PagedDataSource

Private Function GetData() As DataTable
    Dim DT As New DataTable
    Dim CategoryID As Integer = -1
    If IsNothing(ddlCategories.SelectedValue) OrElse Not Integer.TryParse  (ddlCategories.SelectedValue, CategoryID) Then
        CategoryID = -1
    End If

    Dim myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
    Dim myAdapter As New SqlDataAdapter

    myAdapter = New SqlDataAdapter("SearchResources", myConnection)
    myAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

    If SearchText <> String.Empty Then
        trFiltered.Visible = True
    End If

    With myAdapter.SelectCommand.Parameters
        .AddWithValue("@CategoryID", CategoryID)
        .AddWithValue("@SearchText", SearchText)
        .AddWithValue("@CompanyID", CompanyID)
    End With

    If Not Security.IsSiteAdmin Then
        myAdapter.SelectCommand.Parameters.AddWithValue("@UserIsAdmin", 0)
        myAdapter.SelectCommand.Parameters.AddWithValue("@FTPUserID", Security.GetUserID(Context.User.Identity.Name))
    Else
        myAdapter.SelectCommand.Parameters.AddWithValue("@UserIsAdmin", 1)
    End If

    Try
        myAdapter.Fill(DT)
    Catch ex As Exception
        ErrorLog.LogError(ex.Message, ex.StackTrace, HttpContext.Current.User.Identity.Name, HttpContext.Current.Request.Url.ToString)
        HttpContext.Current.Response.Redirect("~/error.aspx", False)
        Return Nothing
    End Try
    Return DT
End Function

Protected Sub ReloadData()
    CurrentPage = 0
    CheckFilters()
    BindData()
End Sub

手头的任务是删除所有会话变量。在不使用会话的情况下表示这些数据的最佳方式是什么?最初我被告知将所有会话项目放入一个 cookie 中,虽然这适用于大多数项目,但由于 cookie 大小限制,它不适用于此,我也不热衷于将其保留在 ViewState 中的想法,或者即使这是一个选择。我对 VB 很陌生,对重写遗留代码没有太多经验。会话不是一个选项,因为它正在被移动到网络场中,并且粘性会话已关闭,因此它必须在没有会话的情况下工作。

非常感谢任何建议。对不起,如果我问了一个答案很明显的问题。

提前致谢, -詹姆士。

【问题讨论】:

    标签: asp.net vb.net session


    【解决方案1】:

    好吧,有人可能会过来投我一票……但我会说你最好的选择是 ViewState。

    如果您受限于不使用 SessionState 并且遇到 cookie 大小限制,我只能想到另外两个选项(Cache 和 ViewState),我当然不会为此推荐 Cache。

    现在您知道可以使用进程外会话状态(SQL Server 或 StateServer)了吗?

    更多信息可以在这里找到: http://msdn.microsoft.com/en-us/library/ms178586.aspx

    【讨论】:

    • 好的,如果 viewstate 是最好的可用选项,我只是有一个关于 ViewState 的问题。每天 30k 的唯一点击量是否会出现性能问题。这段代码是为一个流量不真实的大客户运行的?如果 VieState 没有性能缺陷,那么我可以沿着这条路走下去,但是只是从顶部尝试它,我确实收到一个错误,告诉我我在视图状态中设置的内容不可序列化。我该如何解决这个问题,并且再次重要的是不会出现性能问题。感谢您的帮助。
    • ViewState 的效果主要是客户端感受到的,因为它增加了页面的整体页面大小。至于它的可序列化方面,你能给我一个具体的代码行吗?
    猜你喜欢
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2012-05-14
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 2011-05-12
    相关资源
    最近更新 更多