ASP.NET – Accessing web page twice from client

What happens if one user tries to access an ASP.NET page twice before the first page is returned to the client? Have a look at the code below:

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("ID") = 1
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Redirect("Default3.aspx")
    End Sub End Class

Partial Class Default2
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("ID") = 2
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Redirect("Default3.aspx")
    End Sub
End Class

Imports System.Threading
Partial Class Default3
    Inherits System.Web.UI.Page

    Dim intTest As Integer = 0

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For intTest = 0 To 10
            Response.Write(Session("ID") & " " & intTest & "<br>")
            Thread.Sleep(1000)
        Next
    End Sub
End Class

Accessing default3.axpx from the same client (PC) concurrently from default.aspx (by clicking button) and default2.aspx (by clicking button) causes the session variable to be the same on both requests (though I set the variable to 1 on the first request and 2 on the second). Is it possible to replicate this behaviour without threading? I believe I have this bug in an asp.net application that does not use threading.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

enter image description here

Your question is not about multi-threading; it is about SessionState.

ASP.NET run time uses lock to avoid overriding same session variables although it can handles multiple requests.

It’s why you are do not see miss-matching results.

Please also look at –

ASP.NET Application and Page Life Cycle

ASP.NET Application Life Cycle Overview

Method 2

It’s important to understand that session information is stored at the application level, and really has nothing directly to do with a page.

You could compare it to a global variable in a class. If all your methods are reading and writing to this variable, it will always contain the information from the last time it was updated. It doesn’t matter how many times, or which methods, updated it.

To help see this in your code, create a new class with one property. Change your session variable to be an object of this class. Modify your session logic to refer to this object instead of a string, and update the property.

Finally, put a break point on the setter of your property.

This will let you see whenever your session variable is updated, and with what value. You can also view your stack trace to see what’s setting it.

-note, this is all assuming you aren’t introducing a farm into this and are accessing session from different machines–


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x