Browse DevX
Sign up for e-mail newsletters from DevX


Client- and Server-side Solutions to Managing State in ASP.NET : Page 5

Whether you are building a traditional Windows application or a Web-based application, state is what an application knows about the user, their current interaction with the application, and other pieces of global information. Get a full assessment of the best techniques for managing state.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Server-Based Technique #1: Application State
Now that you understand the client-based state management techniques let's explore the server-based state management techniques. We'll begin with application state.

You must use relative path references when you use cookieless sessions.
I'll start by introducing you to application state variables. They are similar to global variables in traditional Windows applications because they are available from anywhere in the application. What makes them different from Windows application global variables is that they are available to every browser session running the application. You can think of application state variables as a non-existent type of global variable in Windows applications that is available to not only the current user, but to all users running the application. Typically, application state variables contain fairly static data that multiple sessions will share.

You add values to the application state with code like this.

Application("ApplicationName") = "My Application" Application("ApplicationStart") = Now"

You can add objects of any type to application state. You can retrieve application state variables like this.

Dim AppName As String = Application("ApplicationName")

And you use code like this to remove application state variables.


You can remove all the variables from application state by using the RemoveAll method.


Because application state variables are shared across the entire application, there is a possibility that multiple users could attempt to update the same application state variable at the same time. This would not be a good thing. Fortunately Microsoft provided the Lock and Unlock methods to help get around this problem.

The Lock method locks the application state object so that only a single user can update the data. It makes sense to conclude that Unlock releases the Lock, which it does.

Application.Lock Application("PagesDisplayed") += 1 Application.Unlock

You need to understand that an application session starts when the first person accesses the application. In other words, the application does not run if no one visits the site. Once that first visitor shows up and launches the home page for the first time, the application starts.

An application session starts when the first person accesses the application.
There are a number of events related to the Application object contained in the global.asax file. Discussing these events is beyond the scope of this article but I suggest you take a look at them and understand what they do and when they are raised.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date