【问题标题】:Better Explanation for Sessions Asp.Net会话 Asp.Net 的更好解释
【发布时间】:2011-12-08 06:46:37
【问题描述】:

我是第一次尝试使用会话,想以一种更好、更简单的方式了解这一点。

我正在使用 GUID 创建一个会话变量,并使用该 GUID 创建一个文件夹并存储该值,如下所示

 If Session("tempDir") Is Nothing Then
        Dim tempDir As String
        tempDir = Path.GetRandomFileName()
        tempDir = tempDir.Substring(0, tempDir.LastIndexOf("."))
        IO.Directory.CreateDirectory(Server.MapPath("Uploads/" & tempDir))
        IO.Directory.CreateDirectory(Server.MapPath("Downloads/" & tempDir))
        Session.Add("tempDir", tempDir)
        currentDirectory.Value = Session("tempDir").ToString
        CopySession.Text = currentDirectory.Value
    End If

这是生成GUID的代码:

 function randomString(length) {
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');

            if (!length) {
                length = Math.floor(Math.random() * chars.length);
            }

            var str = '';
            for (var i = 0; i < length; i++) {
                str += chars[Math.floor(Math.random() * chars.length)];
            }
            return str;
        }

我使用下面的代码在页面上获取回发但每当我刷新它时都会删除该值并给我一个 NULL 值。

If Page.IsPostBack Then
            If Session("tempDir") Is Nothing Then
                Dim tempDir As String
                tempDir = Path.GetRandomFileName()
                tempDir = tempDir.Substring(0, tempDir.LastIndexOf("."))
                IO.Directory.CreateDirectory(Server.MapPath("Uploads/" & tempDir))
                IO.Directory.CreateDirectory(Server.MapPath("Downloads/" & tempDir))
                Session.Add("tempDir", tempDir)
                currentDirectory.Value = Session("tempDir").ToString
                CopySession.Text = currentDirectory.Value

            End If
End If

我如何retrieve tempDir 值?谁能给我一些详细的解释,因为我完全糊涂了。

【问题讨论】:

    标签: asp.net session refresh


    【解决方案1】:

    从 IF 中删除这 2 行

            currentDirectory.Value = Session("tempDir").ToString
            CopySession.Text = currentDirectory.Value
    

    将会

    If Page.IsPostBack Then
                If Session("tempDir") Is Nothing Then
                    Dim tempDir As String
                    tempDir = Path.GetRandomFileName()
                    tempDir = tempDir.Substring(0, tempDir.LastIndexOf("."))
                    IO.Directory.CreateDirectory(Server.MapPath("Uploads/" & tempDir))
                    IO.Directory.CreateDirectory(Server.MapPath("Downloads/" & tempDir))
                    Session.Add("tempDir", tempDir)
                End If
    
                currentDirectory.Value = Session("tempDir").ToString
                CopySession.Text = currentDirectory.Value
    End If
    

    这里有什么不同,如果会话不存在,那么去设置它。如果 tempDir 的会话存在,因为您刚刚设置了它,则从以前的或其他调用中获取它。我希望这能给你你想知道的,或者告诉我你不明白的地方。

    会话

    会话是一个Dictionary 值,在会话处于活动状态(例如20 分钟)期间与每个用户相关联。当用户与站点交互时,此数据将跟随此用户,您可以使用会话设置、读取或删除它们。 当页面加载开始时读取的会话数据时,您可以在页面上使用它们,并在页面上将保存的会话数据卸载回会话保持器。

    【讨论】:

    • @Aristos-感谢您的解释。我仍然有点困惑,因为不是您的代码,但是会发生什么是我保留了断点并检查了页面回发代码它没有读取会话再次它只是失去它。我需要为此添加任何 global.aspx 页面吗?并添加任何 web.config 部分?
    • @Aristos-Sorry-我已经添加了这个代码两次,没有改变另一个。工作得很好!
    • @DotNetter 如果您使用 InProc 会话而不是 SQL 会话,则很有可能在每次重新加载应用程序时重置它。
    猜你喜欢
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2010-10-27
    相关资源
    最近更新 更多