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:
- Open your project in Visual Studio 2005
- Add New Item from the context menu (right clicking on your project)
- Select the Global Application Class
- Make sure your Global Application Class file (the .asax file) is opened
- Delete all the contents of this .asax file
- Add the following to this .asax file: <%@ Application Language=”C#” Inherits=”Global” %>
- Create a new class named Global in the App_Code directory
- 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) { } }
- 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!