更好地解释会话 Asp.Net
我是第一次尝试使用会话,并且想以更好、更简单的方式了解这一点。
我正在使用 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
我如何检索
tempDir 值?任何人都可以给我一些关于此的详细解释,因为我完全感到困惑。
I am trying to use sessions for the first time and would like to know abt that in a better and easy way.
I am creating a session variable using GUID and creating a folder with that GUID and storing that value as shown below
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
This is the code for generating 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;
}
I am using the below code to get that on page is postback but whenver I refreshes it deletes that value and gives me a NULL 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)
currentDirectory.Value = Session("tempDir").ToString
CopySession.Text = currentDirectory.Value
End If
End If
How do I retrieve
the tempDir
value?Can anyone give me some detailed explanation regarding this as I am totally confusing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
把这2行去掉,
这里有
什么不同,如果会话不存在,那么去设置它。如果 tempDir 的会话存在,因为您刚刚设置了它,则从之前或其他调用中获取它。我希望这能解答您的疑问,或者告诉我您不明白的内容。
会话
会话是与每个用户连接的值的字典会话处于活动状态的时间(例如 20 分钟)。当用户与网站交互时,这些数据将跟随该用户,您可以使用会话设置、读取或删除它们。
当页面加载开始时读取的会话数据时,您可以在页面上使用它们,并在页面上卸载保存回会话保持器的会话数据。
Remove this 2 lines out of the IF
will be
What Is the different here, that if the session is not exist, then go and set it it. After the if the session for tempDir exist because ether you just set it, ether get it from previous or other call. I hope this give you what you wondering, or else tell me what you do not understand.
Session
The session is a Dictionary of values that is connected with each user for the time the session is active (eg for 20 minutes). As the user interacts with the site this data is follow this user and you can set them, read them or delete them using the session.
When a page loads the session data read at the start, you can used them on page, and on page unload the session data saved back to session keeper.