Building WebApps  «Prev  Next»

Lesson 3Using the Global.asa file with applications
ObjectiveSpecify Application start and end procedures in the Global.asa file.

Using the Global.asa File with Applications

Sometimes you will want to create objects that are available to all the users in an application. As with Session variables, you will want to initialize those objects and maybe work with some when an application begins. You may want to initialize an Application variable as well, which you do before the application runs.
The Global.asa file also lets you define code to initialize (or finish) use of an application. Recall that ASP looks for the Global.asa file in the root directory of the application.

Implementing an Application variable

For example, suppose you wanted to track the number of users who are visiting your site at any given time. You'd need to do this in several steps:
  1. Initialize an Application variable in the Application_OnStart procedure (so it is initialized when the first user arrives).
  2. For each new user session started, increment the value in the Application variable.
  3. When each session ends, decrement the value in the Application variable.
  4. Get the value of the Application variable when you need it.
  5. You already know that ASP can detect a session start and end and execute a procedure in the Global.asa file. ASP can also detect both the first use of an application and when it is no longer in use; it will look to the Global.asa file for procedures in both these cases.

Filename: GLOBAL.ASA
Sub Application_OnStart
  Application("aiUserCount") = 0
End Sub

Sub Session_OnStart
  Application("aiUserCount") = Application("aiUserCount") +1
End Sub
Sub Session_OnEnd
  Application("aiUserCount") = Application("aiUserCount") –1
End Sub

ASP.NET Core 3
Here is the same code segment with an explanation:
Global Asa
global.asa File Setup - Example
The next lesson describes some other uses for application variables.