• 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);
                }
            }
        }
  • 相关阅读:
    Object.create() 实现
    Subpub 订阅/发布
    闭包的一种用法
    console 高级用法
    instanceof typeof
    reg 正则
    zepto js 源码 解读
    【Pro ASP.NET MVC 3 Framework】.学习笔记.8.SportsStore:管理
    【Pro ASP.NET MVC 3 Framework】.学习笔记.7.SportsStore:购物车
    【Pro ASP.NET MVC 3 Framework】.学习笔记.6.SportsStore:导航
  • 原文地址:https://www.cnblogs.com/yonsy/p/7568495.html
Copyright © 2020-2023  润新知