曹永思

导航

asp.net 抽象方法和虚方法的用法区别,用Global类重写Application_BeginRequest等方法为例子

不废话,直接贴代码

  public abstract class LogNetGlobal : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            Application_Start_();
        }
        //public static void RegisterRoutes(RouteCollection routes)
        //{
        //    InitRoutes.SetStaticConfig(".html");
        //    InitRoutes ir = new InitRoutes(routes);
        //    ir.LoadRoutes(null);
        //}
        public void Application_BeginRequest(object sender, EventArgs e)
        {
            Application_BeginRequest_(sender, e);
            LogNet.LogNetHttpContext.Items.AddBeginTime();
        }

        public void Application_EndRequest(object sender, EventArgs e)
        {
            #region 将请求信息存入日志 

            string url = Request.Url.AbsoluteUri;
            if (!url.ToLower().Contains("testpostjson.aspx") && !url.ToLower().Contains("readlog.aspx"))
            {
                LogNet.Log.WriteLog(Request);
            }
            #endregion
            Application_EndRequest_(sender, e);
        }
        public void Application_Error(object sender, EventArgs e)
        {
            HttpApplication httpApp = sender as HttpApplication;
            if (httpApp != null && httpApp.Context != null && httpApp.Context.Error != null)
            {
                LogNet.Log.WriteLog(httpApp.Context.Error);
            }
            Application_Error_(sender, e);
        }

        /// <summary>
        /// 抽象方法不能有方法体,子类必须要重写该方法
        /// </summary>
        protected abstract void Application_Start_();
        /// <summary>
        /// 虚方法一定要有方法体,子类可以选择重写该方法,可以通过base.Application_BeginRequest_(sender,e)的方式调用该基方法
        /// </summary>
        protected virtual void Application_BeginRequest_(object sender, EventArgs e) { }
        protected virtual void Application_EndRequest_(object sender, EventArgs e) { }
        protected virtual void Application_Error_(object sender, EventArgs e) { }
    }

    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
    // 请访问 http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : LogNetGlobal
    {
        protected override void Application_Start_()
        {
            AreaRegistration.RegisterAllAreas();
            // RegisterRoutes(RouteTable.Routes); 

            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Insert(0, new BaseRazorViewEngine());

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            OAWebLib.InPack myInpack = new OAWebLib.InPack();
            myInpack.Params.AddString("conn_string", Config.OADsn);
            OAWebLib.OACore.InitialCore(myInpack, OAWebLibFunc.Instance);
            Config.Init_Config();
            Config.is_open_tj = false;


            Global_Config.InitConfig();
        }
        public static void RegisterRoutes(RouteCollection routes)
        {
            InitRoutes.SetStaticConfig(".html");
            InitRoutes ir = new InitRoutes(routes);
            ir.LoadRoutes(null);
        }
        protected override void Application_BeginRequest_(object sender, EventArgs e)
        {
            base.Application_BeginRequest_(sender, e);

        }

        protected override void Application_EndRequest_(object sender, EventArgs e)
        { 
        }
        protected override void Application_Error_(object sender, EventArgs e)
        {
            HttpApplication httpApp = sender as HttpApplication;
            if (httpApp != null && httpApp.Context != null && httpApp.Context.Error != null)
            {
                LogNet.Log.WriteLog(httpApp.Context.Error);
                ClassLoger.Error(httpApp.Context.Error, httpApp.Context.Error.Message);
            }
        }
    }

 

posted on 2017-09-21 14:33  曹永思  阅读(196)  评论(0编辑  收藏  举报