Building WebApps  «Prev  Next»

Lesson 2Running a Web application
ObjectiveDescribe 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.


ASP.NET Core 5

Starting and running a Web 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.
Diagram of ASP web application
The image illustrates a hierarchical directory structure typically used in a web application or content management system (CMS).
Here's an analysis of its nature and filesystem structure:
Key Characteristics:
  1. Root Directory:
    • The structure starts from the "Application Root," which represents the top-level directory of the application or website.
    • This is likely the main folder where all application-related files and directories are stored.
  2. Subdirectories and Files:
    • Admin: This folder may contain administrative files or scripts used for backend operations, user management, or site configuration.
    • Content: Likely contains dynamic or static web pages, documents, or text-based content for the application.
    • Classes: This folder is probably used to store code modules, libraries, or class definitions, especially in object-oriented programming languages like ASP, PHP, or Python.
    • Media:
      • A nested folder structure under "Media" categorizes different types of media files:
        • Sound: For audio files (e.g., MP3, WAV).
        • Images: For image assets (e.g., PNG, JPG, GIF).
        • Video: For video files (e.g., MP4, AVI).
    • default.htm: A common default page for web servers, serving as the homepage or a landing page for the application.
    • global.asa: A configuration file used in classic ASP (Active Server Pages) applications. It typically contains application-level or session-level event handlers and settings.
  3. File System and Organizational Practices:
    • The structure reflects a well-organized and modular approach, where similar types of resources are grouped into specific folders.
    • This hierarchical organization improves maintainability, scalability, and collaboration among developers.
  4. Potential Platform:
    • Given the presence of global.asa, this is likely associated with a classic ASP application running on a Windows-based IIS (Internet Information Services) server.
    • The use of default.htm suggests compatibility with standard web server settings for default document handling.

Observations:
  • The directory structure supports a typical web application, with clear separation of concerns (e.g., admin tools, media, content, and logic/code files).
  • It implies a focus on ease of development, media management, and user interface design.

The following series of images illustrates how ASP manages an application and its multiple users:



Running and managing ASP Application
1) Web Server is turned on, ASP is running, and the Global.asa file is in the root directory.
1) Web Server is turned on, ASP is running, and the Global.asa file is in the root directory. There are currently no users connected to the website.

2) A user directs her browser to the home or default page of our web site
2) A user directs her browser to the home or default page of our web site. ASP automatically runs the Global.asa file. If there is a procedure in Global.asa to run prior to starting the web application (called Application_OnStart), any code in that procedure will be executed.

3) The first user starts the application and begins her own session
3) The first user starts the application and begins her own session. ASP again goes to Global.asa and looks for, and runs, any Session_onStart procedure.


4) If another user connects to the website while the first user is still on the website, a session is started for this user. Since the application has been started
4) If another user connects to the website while the first user is still on the website, a session is started for this user. Since the application has been started, ASP does not rerun the Application_OnStart procedure. As users connect to and disconnect from the website, additional sessions are created. However, all users share the same application object and application variables.

5) As user sessions expire, ASP runs the Session_OnEnd procedure.
5) As user sessions expire, ASP runs the Session_OnEnd procedure, if there is one, in the Global.asa file and shuts down the sessions. The application continues to run as long as at least one user is connected to the website.

6) When the last user's session expires and ASP has run the Sesion_OnEnd
6) When the last user's session expires and ASP has run the Sesion_OnEnd procedure for it, any Application_OnEnd procedure stored in Global.asa is run. The application is terminated and all application variables are erased, although the Web server continues to run.

7) The website is again ready for the next user(s) to connect to it.
7) The website is again ready for the next user(s) to connect to it.


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.
  1. 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.
  2. 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.
  3. Compilation
    • Unlike Global.asa, which was interpreted, Global.asax is compiled into a .NET assembly, which improves performance.
  4. Inheritance
    • The file inherits from the HttpApplication class, giving it access to ASP.NET’s event-driven programming model.
  5. 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:
  1. Startup.cs file:
    • Contains ConfigureServices and Configure methods for setting up middleware, services, and routing.
  2. 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.

SEMrush Software