ASP中关于全局页面的作用 asax文件

  如果给ASP添加应用程序添加一个新项,就会进入Add New Iteam对话框。在这个对话框中可以给应用程序添加一个Global Application Class。这会添加一个Global.asax文件。该文件为该应用程序的全局变量,保存应用程序的的事件,对象和变量,所有这些都可以在应用程序的返回内访问。

  与aspx文件页面中页面级的事件一样,也可以在asax文件中出来应用程序中的事件。其中有几个重要的方法。

  1、Application_Start:在应用程序接受到第一个请求时调用,这是在应用程序中给应用程序级的变量赋值或者指定所有用户都必须维护的状态理想位置。

  2、Session_Start:类似于Application_Start事件,但

是这个事件在用户第一次访问应用程序时调用。例如Application_Start事件只在接收到第一个请求时引发,第一个请求会让应用程序运行,而Session_Start事件会在每个终端用户第一次向应用程序发出的请求时调用。

  3、Application_BegionRequest:它没有列在VS2008模板中,但该事件会在每个请求发出之前引发,也就是说在请求到达服务器,且得到处理之前,会引发Application_BeginingRequest事件并在处理该事件之前处理该事件。

  4、Application_AuthenticateRequest:每个请求都

会触发此事件,允许用户为请求建立定制的身份验证。

  5、Application_Error:在应用程序的用户抛出错误的时候触发。它适合用于提供应用程序级的错误的处理,或者把错误记录在指定的文件夹内的txt文件中,这样可以在调试的时候进行查找错误的存在的页面的地址与错误的信息。或者把错误记录到服务器中的事件日志中。

  6、Application_End:在应用程序结束的

时候触发该事件,大多数的程序员不常使用此事件,因为ASP.NET已经能很好的完成了关闭和清理剩余对象的任务。当然在应用程序运行的时候进行修改web.config文件,这会引发Application_End事件,并在事件日志中添加日志。

以下是一个是一个完整的例子:使用与调试程序,运行程序的时候进行调试会把错误写在指定文件内,利于程序的调试,发现错误的存在。

     /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private IContainer components = null;
        public Global()
        {
            InitializeComponent();
        }    
        protected void Application_Start(object sender, EventArgs e)
        {
            //进入系统的时间
            Application["AppStartTime"] = DateTime.Now;
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            //对session缓存的记忆
            Session["Style"] = 1;
            Session.Timeout = 180; 
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        void Application_Error(Object sender, EventArgs e)
        {
            Exception exception1 = this.Server.GetLastError().GetBaseException();
            if (exception1.GetType() == typeof(HttpException))
            {
                int i = ((HttpException)exception1).GetHttpCode();
                if (i == 404)
                {
                    //跳转到错误页面
                    Response.Redirect("~/error.htm", false);
                    return;
                }
            }

            string errortime = "发生时间:" + DateTime.Now.ToString();
            string errorAddress = "发生页面:" + Request.Url.ToString();
            string errmessage= "异常信息:" + exception1.Message;
            string errorsource = "错误源:" + exception1.Source;
            Server.ClearError();

            System.IO.StreamWriter writer = null;
            try
            {
                lock (this)
                {
                    string year = DateTime.Now.Year.ToString();
                    string month = DateTime.Now.Month.ToString();
                    string day = DateTime.Now.Day.ToString();
                    string path = string.Empty;
                    string filename = DateTime.Now.ToString("yyyyMMdd") + ".txt";
                    / /将错误保存到指定的文件中获取用户的信息
                    path = Server.MapPath("~/Error/") + year + month + day;
                    if (!System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }
                    System.IO.FileInfo fileinfo = new System.IO.FileInfo(path + "/" + filename);
                    writer.WriteLine("        用        户        IP:" + Request.UserHostAddress);
                    writer.WriteLine(errortime);
                    writer.WriteLine(errorAddress);
                    writer.WriteLine(errinfo);
                    writer.WriteLine(errorsource);
                    writer.WriteLine(errortrace);
                }
            }
            catch
            {

            }
            finally
            {
                if (writer != null)
                    writer.Close();
            }
            Response.Redirect("~/error.htm", false);
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
        #region Web 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new Container();
        }
        #endregion

 

posted @ 2013-04-11 21:01  Summay  阅读(941)  评论(0编辑  收藏  举报