Application State in ASP.NET

Application State

How ASP.NET Works?

In ASP we see all of our files are saved in the same place either they are our code files, script files, master pages, etc. In short, these files, create a virtual directory on the web server. And we also know HTTP is a stateless protocol in simple words when a user request a page using HTTP protocol an instance is created and the main page is converted into HTML and return to the user and after this instance will destroy just because HTTP does not maintain state, it’s a stateless protocol. But if we have to maintain state, then what to do? For this in asp.net state management techniques are used. In this tutorial, you will learn, how to maintain Application State in ASP.NET.

So in ASP there are 4 four types of techniques to maintain the state:

View state

Session State

Application state

Control State

Application State in ASP.NET

Today we will discuss the Application state:

As in asp.net all files combine and make a virtual directory so in this case when information is stored in application state, it’s easy for all the users uses it, in other words, its available for all the users.

Asp.net creates an object with the class named httpApplicationstate and creates an object for all the users and save that class in global.asax file. This file is saved in server memory.

components of Application State
Components of Application State

The application state is used to save hit counts, tax, discount rate etc. As we know the session state is only for one user, but the application state is available for all users. There is no default expiry duration of application state. This is only destroyed when we close the running process. The data in application state is saved in key value pair and also be accessed using the httpContext class property.

Application state data is maintained by defining the handlers for the following functions:

Application_Start(): this function is called when the first time domain is created.

Session_Start(): this function is called whenever a session needs to start.

Application_BeginRequest(): when an application is started, and the first method executes, then this function is executed for every user.

Application_AuthenticateRequest(): this function validate users.

Application_Error(): this function is used to handle errors and exceptions.

Session_End(): when we need to destroy session this function is called.

Application_End(): it executes before the end of the application.

Application_Disposed(): it’s the last function call after the application is shut down.

 

A Count for online users using Application state