Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the Global.asax.cs file?

I am using VS 2008. I have created a new Asp.net web site project from File->New->Website->Asp.net Website.

Now I want to add the Global.asax as well as the .cs file to the project. So I Right click on the project ->Add New Item->Global Application Class. Then I clicked on the add button.

The Global.asax file got added with the content as under

<%@ Application Language="C#" %>   <script runat="server"%>      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)      {         // Code that runs when a session ends.          // Note: The Session_End event is raised only when the sessionstate mode         // is set to InProc in the Web.config file. If session mode is set to StateServer          // or SQLServer, the event is not raised.      }  </script> 

This is fine. But where is the Global.asax.cs file?

Thanks

like image 867
apurva Avatar asked Jun 09 '11 04:06

apurva


People also ask

How do I add global asax Cs to my website?

How to add global. asax file: Select Website >>Add New Item (or Project >> Add New Item if you're using the Visual Studio web project model) and choose the Global Application Class template. After you have added the global.

What is global asax Cs in MVC?

The Global. asax file is a special file that contains event handlers for ASP.NET application lifecycle events. The route table is created during the Application Start event. The file in Listing 1 contains the default Global. asax file for an ASP.NET MVC application.

Where is global asax file in ASP.NET core?

There is no global. asax file in . NET Core and most of the things that we were doing in global.


1 Answers

It don't create normally; you need to add it by yourself.

After adding Global.asax by

  • Right clicking your website -> Add New Item -> Global Application Class -> Add

You need to add a class

  • Right clicking App_Code -> Add New Item -> Class -> name it Global.cs -> Add

Inherit the newly generated by System.Web.HttpApplication and copy all the method created Global.asax to Global.cs and also add an inherit attribute to the Global.asax file.

Your Global.asax will look like this: -

<%@ Application Language="C#" Inherits="Global" %> 

Your Global.cs in App_Code will look like this: -

public class Global : System.Web.HttpApplication {     public Global()     {         //         // TODO: Add constructor logic here         //     }      void Application_Start(object sender, EventArgs e)     {         // Code that runs on application startup      }     /// Many other events like begin request...e.t.c, e.t.c } 
like image 89
Waqas Raja Avatar answered Sep 19 '22 20:09

Waqas Raja