Describe the setup and operation of a Web application
Setup and Operation of ASP Web Application
One of the critical differences between typical standalone applications and a Web application is the HTTP's lack of state. In the last module, we discussed how you could use the Session object to keep track of information for a specific user and maintain state. ASP also includes an Application object that enables you to track variables and share information among all the users who are using the same application at the same time. Still, when users are constantly requesting and receiving Web pages, but often not talking to the server, how do you define "application" in this context?
Setting up directories for Web Application
When using Active Server Pages, an application is defined as all of the .asp and .html files in a virtual directory and its subdirectories.
The following diagram illustrates one possible organization for the T-Mania Web site and highlights its catalog ASP application.
In addition to organizing the various elements of your Web site, using multiple subdirectories allows you to assign a higher level of security to directories containing user data and administrative files. Through ASP, you can start and initialize a Web application when the first user accesses it. Any activities that have to occur before the application is run, such as writing to a log file or opening a database, can be performed through the Global.asa file that you learned about in the module on session management.
The following series of images illustrates how ASP manages an application and its multiple users:
Running and managing ASP Application
You already know how to set up special procedures in the Global.asa file that are run whenever users begin and end their sessions.
Now you will learn how to set up similar procedures for applications. The next lesson will explain how to specify application start and end procedures.
Key Features of Global.asax in ASP.NET
ASP.NET uses the Global.asax file for application-level events and configurations, replacing the older Global.asa file from Classic ASP. The Global.asax file, also known as the ASP.NET Application File, is a special file in an ASP.NET application that allows you to define and handle events at the application level, such as application startup, shutdown, or session start.
Event Handling
The Global.asax file contains methods for handling application-level events. Commonly used events include:
Application_Start: Runs when the application starts.
Application_End: Runs when the application ends.
Session_Start: Runs when a new user session starts.
Session_End: Runs when a user session ends.
Application_Error: Handles unhandled errors in the application.
Code-behind Model
The logic in the Global.asax file can be written inline or separated into a code-behind file, following the typical ASP.NET separation of markup and logic.
Compilation
Unlike Global.asa, which was interpreted, Global.asax is compiled into a .NET assembly, which improves performance.
Inheritance
The file inherits from the HttpApplication class, giving it access to ASP.NETâs event-driven programming model.
Optional File
The Global.asax file is optional. If itâs not present, the application still runs but lacks the ability to handle application-level events centrally.
Location and Format:
The Global.asax file resides in the root of the web application directory.
It typically looks like this:
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code to run on application startup
}
void Session_Start(object sender, EventArgs e)
{
// Code to run on session start
}
void Application_BeginRequest(object sender, EventArgs e)
{
// Code to run on every request
}
void Application_Error(object sender, EventArgs e)
{
// Code to handle errors
}
void Session_End(object sender, EventArgs e)
{
// Code to run on session end
}
void Application_End(object sender, EventArgs e)
{
// Code to run on application shutdown
}
</script>
Advanced Alternatives in Modern ASP.NET Core:
For modern applications using ASP.NET Core, the Global.asax approach is no longer used. Instead, application startup logic is handled in:
Startup.cs file:
Contains ConfigureServices and Configure methods for setting up middleware, services, and routing.
Program.cs file:
Used to configure and start the applicationâs web host.
ASP.NET Core's design is more modular, flexible, and adheres to modern dependency injection and middleware principles.