Asp.NET Application State Management

Application State Management

It is the structure used to hold data about the entire site. The data here is valid for the entire site and all visitors, and we can access the information here from the desired page and take action.

The Global.Asax file is used to obtain and process Application State Management information in Asp.NET.

What Is a Global.Asax File?

The Global.asax file is an optional file that allows us to capture and act on application-level information. It is secure because it works server-side and cannot be accessed by the client.

The first example that comes to mind when it comes to Application State Management is the display of the number of online users. Apart from that, we can perform many things in this file, such as the actions we want to be performed when a visitor starts a session.

If there is no Global.asax file in our project, we can add it using the Add New Item window, with the Global Application Class option.

In this file, we can provide action in desired situations by means of various event programs.

protected void Application_Start(object sender, EventArgs e)
It is called as soon as our application (our website) is first launched on the server.

protected void Application_End(object sender, EventArgs e)
Called when our application has stopped.

protected void Application_Error(object sender, EventArgs e)
Invoked when an error occurs while the application is running. Operations such as keeping error records can be done.

protected void Application_BeginRequest(object sender, EventArgs e)
It works when a request comes to any page on our site. We can provide different actions depending on the type or status of the incoming request.

protected void Application_AuthenticateRequest(object sender, EventArgs e)
In a membership-based site, it is an event that is triggered when the user successfully logs into the system. In this way, it is possible to direct the user to a page, assign a role, leave a cookie on his computer, etc.

protected void Session_Start(object sender, EventArgs e)
It works if a visitor visits any page on our site. It works as soon as a request comes to the page. It will not run again as long as the user stays on that page, but if a new request comes after the session expires, this event will run again. This event is also used to calculate the number of online visitors.

protected void Session_End(object sender, EventArgs e)
This event is triggered when the visitor's session expires. For example, while calculating the number of online users, it is increased in Session_Start event and decreased in Session_End event. In this way, the total number of users currently logged in can be found.

Asp.NET Application State Management, using Application State Management, displaying the number of online visitors, what is global.asax, application information storage on asp.net server

EXERCISES

Calculating and Displaying the Number of Online Visitors

Edit the Global.Asax file as follows:

<%@ Application Language="C#" %>
<%@ Import Namespace="site_webprogramciligi" %>
<%@ Import Namespace="System.Web.Optimization" %>
 
<script runat="server">
 
    void Application_Start(object sender, EventArgs e)
    {
        // This event is triggered the first time the application runs on the server. Here we set the UserNumber parameter to 0.
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterOpenAuth();
 
        Application["UsersCount"] = 0;
    }
    
    void Application_End(object sender, EventArgs e)
    {
        //  Codes that will run when the application is stopped
 
    }
 
    void Application_Error(object sender, EventArgs e)
    {
        // Codes to run when an error occurs
 
    }
 
    void Session_Start(object sender, EventArgs e)
    {
        // The codes that will run when the session starts by entering any page. Here we increase the UserNumber variable by 1. This number will increase by 1 with each new session.
 
        Application["UserNumber "] = ((int)Application["UserNumber"]) + 1;
    }
 
    void Session_End(object sender, EventArgs e)
    {
        // Codes that will run when the user's session ends. Here we reduce the UserNumber variable by 1. This number will decrease by 1 for each closed session.
 
        Application["UserNumber"] = ((int)Application["UserNumber"]) - 1;
    }
</script>

Add The Following Code To The Related Pages To Show The Number Of Online Visitors On Our Pages:

LabelUsersCount.Text = "Online Users: <b>"+Convert.ToString( Application[ "UserNumber" ] )+"</b>";

 



COMMENTS




Read 705 times.

Online Users: 718



asp-net-application-state-management