zoukankan      html  css  js  c++  java
  • 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

    2 Answers

    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()
            {
            }
        }
    }

    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 createdGlobal.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
    }

    Orignal LinkUrl:http://stackoverflow.com/questions/6288209/where-is-the-global-asax-cs-file

  • 相关阅读:
    重装fedora17之后的一些配置
    virtualbox中安装3D支持
    Win8下Qualcomm Atheros AR9285网卡改mac
    Android应用【世界杯知识答题】的界面设计经验
    Android应用【世界杯风云2010】的界面设计经验小结
    html基础知识层级选择器
    html基础知识,两种引入css样式方式
    html结构伪类选择器
    html基础知识表格标签(form,input)
    html基础知识(三种颜色写法)
  • 原文地址:https://www.cnblogs.com/mingjia/p/5519369.html
Copyright © 2011-2022 走看看