48

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

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79
apurva
  • 603
  • 2
  • 6
  • 7
  • 2
    http://stackoverflow.com/questions/6055927/where-is-global-asax-cs-in-visual-studio-2010 :) – naveen Jun 09 '11 at 05:03

2 Answers2

58

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
}
artm
  • 8,554
  • 3
  • 26
  • 43
Waqas Raja
  • 10,802
  • 4
  • 33
  • 38
  • I am getting error "The 'inherit' attribute is not supported by the 'application' directive." – Rahul Jain Oct 13 '14 at 05:30
  • @RahulJain, it is `Inherits` not inherit. did you checked your spellings? otherwise which framework version you are using? – Waqas Raja Oct 13 '14 at 06:50
  • There's a typo in the description (Glabal.cs) - I cannot edit this post because the edit is less than 6 characters. – steadweb Mar 18 '15 at 14:50
32

That's because you created a Web Site instead of a Web Application. The cs/vb files can only be seen in a Web Application, but in a website you can't have a separate cs/vb file.

Edit: In the website you can add a cs file behavior like..

<%@ Application CodeFile="Global.asax.cs" Inherits="ApplicationName.MyApplication" Language="C#" %>

~/Global.asax.cs:

namespace ApplicationName
{
    public partial class MyApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
        }
    }
}
Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • Then what is the way of getting global.asax as well as global.asax.cs file? – apurva Jun 09 '11 at 04:52
  • If you use Web Application model then you will be able to get seperate cs file. – Muhammad Akhtar Jun 09 '11 at 04:54
  • @apurva; Updated answer, you can achieve in the website as well :) – Muhammad Akhtar Jun 09 '11 at 04:58
  • I am getting a complile time error Could not load type 'Global'. I have updated the question – apurva Jun 09 '11 at 05:42
  • Follow my answer step by step and check it and try to do in the same way. you will be able to resolve the issue. – Muhammad Akhtar Jun 09 '11 at 05:44
  • @apurva what you do it: right-click the website in the Solution Explorer and select "Add new item", then select "Global Application Class" from the templates. A file named Global.asax will created be in the root of the site. The item "Global Application Class" template will only be in the list if you haven't created one yet. – zanlok Jun 28 '11 at 18:37
  • 1
    I got "cannot load type XXXX" error. I have it as a website project. – Chih-Ho Andy Chou Jan 07 '16 at 14:41
  • You need also move the Global.asax.cs to the App_Code subfolder. I compiled it successfully. – ligand Jun 23 '21 at 08:57