Where is the Global.asax.cs file?

后端 未结 2 1969
旧时难觅i
旧时难觅i 2020-12-05 04:07

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

相关标签:
2条回答
  • 2020-12-05 04:37

    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()
            {
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 05:01

    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
    }
    
    0 讨论(0)
提交回复
热议问题