Getting Started
  Introduction
  What is ASP.NET?
  Language Support

ASP.NET Web Forms
  Introducing Web Forms
  Working with Server Controls
  Applying Styles to Controls
  Server Control Form Validation
  Web Forms User Controls
  Data Binding Server Controls
  Server-Side Data Access
  Data Access and Customization
  Working with Business Objects
  Authoring Custom Controls
  Web Forms Controls Reference
  Web Forms Syntax Reference

ASP.NET Web Services
  Introducing Web Services
  Writing a Simple Web Service
  Web Service Type Marshalling
  Using Data in Web Services
  Using Objects and Intrinsics
  The WebService Behavior
  HTML Pattern Matching

ASP.NET Web Applications
  Application Overview
  Using the Global.asax File
  Managing Application State
  HttpHandlers and Factories

Cache Services
  Caching Overview
  Page Output Caching
  Page Fragment Caching
  Page Data Caching

Configuration
  Configuration Overview
  Configuration File Format
  Retrieving Configuration

Deployment
  Deploying Applications
  Using the Process Model
  Handling Errors

Security
  Security Overview
  Authentication & Authorization
  Windows-based Authentication
  Forms-based Authentication
  Authorizing Users and Roles
  User Account Impersonation
  Security and WebServices

Localization
  Internationalization Overview
  Setting Culture and Encoding
  Localizing ASP.NET Applications
  Working with Resource Files

Tracing
  Tracing Overview
  Trace Logging to Page Output
  Application-level Trace Logging

Debugging
  The SDK Debugger

Performance
  Performance Overview
  Performance Tuning Tips
  Measuring Performance

ASP to ASP.NET Migration
  Migration Overview
  Syntax and Semantics
  Language Compatibility
  COM Interoperability
  Transactions

Sample Applications
  A Personalized Portal
  An E-Commerce Storefront
  A Class Browser Application
  IBuySpy.com

  Get URL for this page

Using the Global.asax File


The Global.asax File

In addition to writing UI code, developers can also add application level logic and event handling code into their Web applications. This code does not handle generating UI and is typically not invoked in response to individual page requests. Instead, it is responsible for handling higher-level application events such as Application_Start, Application_End, Session_Start, Session_End, and so on. Developers author this logic using a Global.asax file located at the root of a particular Web application's virtual directory tree. ASP.NET automatically parses and compiles this file into a dynamic .NET Framework class--which extends the HttpApplication base class--the first time any resource or URL within the application namespace is activated or requested.

The Global.asax file is parsed and dynamically compiled by ASP.NET into a .NET Framework class the first time any resource or URL within its application namespace is activated or requested. The Global.asax file is configured to automatically reject any direct URL request so that external users cannot download or view the code within.

Application or Session-Scoped Events

Developers can define handlers for events of the HttpApplication base class by authoring methods in the Global.asax file that conform to the naming pattern "Application_EventName(AppropriateEventArgumentSignature)". For example:

VB

If the event handling code needs to import additional namespaces, the @ import directive can be used on an .aspx page, as follows:

<%@ Import Namespace="System.Text" %>

The following sample illustrates the lifetime of Application, Session, and Request.

 
VB Application1.aspx

[Run Sample] | [View Source]

The first time the page is opened, the Start event is raised for the application and the session:


Sub Application_Start(Sender As Object, E As EventArgs)
  ' Application startup code goes here
End Sub

Sub Session_Start(Sender As Object, E As EventArgs)
  Response.Write("Session is Starting...<br>")
  Session.Timeout = 1
End Sub
VB

The BeginRequest and EndRequest events are raised on each request. When the page is refreshed, only messages from BeginRequest, EndRequest, and the Page_Load method will appear. Note that by abandoning the current session (click the "End this session" button) a new session is created and the Session_Start event is raised again.

Application or Session-Scoped Objects

Static objects, .NET Framework classes, and COM components all can be defined in the Global.asax file using the object tag. The scope can be appinstance, session, or application. The appinstance scope denotes that the object is specific to one instance of HttpApplication and is not shared.

Section Summary

  1. ASP.NET Framework applications can define event handlers with application-wide or session-wide scope in the Global.asax file.
  2. ASP.NET Framework applications can define objects with application-wide or session-wide scope in the Global.asax file.


Copyright 2001 Microsoft Corporation. All rights reserved.

GoToMyPC | ASP.NET Knowledge Base