Home > .NET, ASP.NET, CSharp > Distilled: Fixing the Global.asax in ASP.NET 2.0

Distilled: Fixing the Global.asax in ASP.NET 2.0

June 20th, 2006

Visual Studio 2005 no longer adds a code behind file to the Global.asax file.

To add a code behind file for the Global.asax file (in C#) follow these steps:

  1. Open your project in Visual Studio 2005
  2. Add New Item from the context menu (right clicking on your project)
  3. Select the Global Application Class
  4. Make sure your Global Application Class file (the .asax file) is opened
  5. Delete all the contents of this .asax file
  6. Add the following to this .asax file:  <%@ Application Language=”C#” Inherits=”Global” %>
  7. Create a new class named Global in the App_Code directory
  8. Copy the following source into your new Global class:
    /// Summary description for Global 
    public class Global : System.Web.HttpApplication {
     Global() { }
     void Application_Start(object sender, EventArgs e) {
      // Code that runs on application startup
     }
     void Application_End(object sender, EventArgs e) {
      // Code that runs on application shutdown
     }
     void Application_Error(object sender, EventArgs e) {
      // Code that runs when an unhandled error occurs
     }
     void Session_Start(object sender, EventArgs e) {
      // Code that runs when a new session is started
     }
     void Session_End(object sender, EventArgs e) { }
     }
  9. Save, you are done

The preceding has been distilled from Ross Nelson’s article: Fixing the Global.asax in ASP.NET 2.0.

On a related thread: Consider using HttpModules instead of the Global.asax file, Learn more in Karl Seguin’s article titled: Global.asax? Use HttpModules Instead!

Author: Adam Kahtava Categories: .NET, ASP.NET, CSharp Tags:
  1. No comments yet.
  1. No trackbacks yet.