• Global.asax文件说明


    Global.asax是我们的底层文件,第一次的IIS请求都会先去执行它里面的文件,所以学会它里面的函数是非常有必要的。而且我们总是忽略这里的知识点,总觉得这是不必须的,其实我们错了,这里才是程序的根本。

    文件代码:

        /// <summary>
        /// 所有的应用,状态,程序被访问,用户退出,都可以找到。,
        /// </summary>
        public class Global : System.Web.HttpApplication
        {
            /// <summary>
            /// 这里是IIS请求一开始执行,就执行一遍这个方法。后面还有个Application_End方法。
            /// </summary>
            /// 程序启动的时候都会执行这个函数(一次),相当于Main函数,
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Application_Start(object sender, EventArgs e)
            {
                File.AppendAllText("c:/1.txt",DateTime.Now.ToString()+"Application_Start");
            }
            /// <summary>
            /// 这里的Session是服务器端为每一个浏览器保存数据开辟的临时存储空间,当浏览器关闭或者切换用户就会重新开辟内存来保存Session
            /// 每一个浏览器的访问网页都会有一个有一个Session
            /// 一个浏览器的一个用户公用了一个Session ,当用户主动退出的时候
            /// </summary>
            /// 统计当前在线人数
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Session_Start(object sender,EventArgs e)
            {
                //这里相当于建立的临时会话一样。
                HttpContext.Current.Session.Abandon();//销毁Session   ,15分钟自动取消
            }
            /// <summary>
            /// 请求的时候做一些处理。(每一个应用都会触发这里)        
            /// </summary>
            /// <param name="sender"></param>
            /// 查看当前请求的URL,通过这个(HttpContext.Current.Request.URL)在
            /// 快速监听里面查看总共请求了哪些URL
            /// <param name="e"></param>
            protected void Application_BeginRequest(object sender,EventArgs e)
            {
                //实现功能,屏蔽IP
                if (HttpContext.Current.Request.UserHostAddress== "192.168.1.102")
                {
                    HttpContext.Current.Response.Write("这里就可以把自己电脑的IP地址屏蔽掉了。");
                }
                else
                {
                    HttpContext.Current.Response.Write("");
                }
                //防盗猎
            }
    
            protected  void Application_AuthentiateRequest(object sender,EventArgs e)
            {
    
            }
            /// <summary>
            /// 异常的处理模块
            /// </summary>
            /// 调用这里的函数来曝出异常信息
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Application_Error(object sender,EventArgs e)
            {
    
            }
    
            /// <summary>
            /// 断开会话,15超时的时候调用这个
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Session_End(object sender,EventArgs e)
            {
                
            }
            /// <summary>
            /// 程序被关闭的时候执行一次,IIS被关闭才执行。
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Application_End(object sender,EventArgs e)
            {
    
            }
        }

    函数的说明:

    Global.asax这个文件是执行IIS请求必进过的文件,是非常重要。对于Web应用而言是声明周期的一个事件响应的地方。 Global类,它继承自System.Web.HttpApplication,它的作用是定义 ASP.NET 应用程序中的所有应用程序对象共有的方法、属性和事件。 此类是用户在 Global.asax 文件中所定义的应用程序的基类。*:Application_Start() 此函数是我们程序刚启动的时候调用的函数,相当于我们C语言时候的Main函数一样,都是程序一开始执行的函数,可以将一些需要初始化的函数,方法,写在这里,比如路由,日志,IOC,DI,区域,文件等,关闭的时候有个对应的方法Application_End()函数

            protected void Application_Start()
            {
                EngineContext.Initialize(false);
    
                var dependencyResolver = new ControllerDependencyResolver();
                DependencyResolver.SetResolver(dependencyResolver);
                GlobalConfiguration.Configuration.DependencyResolver = dependencyResolver;
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
            protected void Application_End(object sender,EventArgs e)
            {
    
            }

    *:Application_Error() 异常处理函数,当应用程序中出现未捕获的异常,此函数被调用,这里用HttpContext.Current.Server.GetLastError()来获得异常信息,可以将其保存到log4Net记录到日志中。

            protected void Application_Error(object sender, EventArgs e)
            {
                var exception = Server.GetLastError();
                LogException(exception);
    
            }

    *:Session_Start() 服务端Session信息,这里是每一个浏览器去访问服务器,服务器都会为其创建内存空间,即Session来保存它的一些信息,我们打开的一些网页都在一个Session中进行访问。(好像是15分钟自动掉线,相当于一次时间有限的会话。)一个浏览器的一个用户公用一个Session,当用户主动退出的时候Session就会被关闭,调用下面的函数来关闭它,Session_End()。可以在它里面来统计当前在线人数等。

            HttpContext.Current.Session.Abandon();//销毁Session   ,15分钟自动取消
            protected void Session_Start(object sender,EventArgs e)
            {
                //这里相当于建立的临时会话一样。
                HttpContext.Current.Session.Abandon();//销毁Session   ,15分钟自动取消
            }
            protected void Session_End(object sender,EventArgs e)
            {
                
            }

    *:Application_BeginRequest() 请求的时候都会访问这个函数,每一个应用也会触发这里,我们可以通过下面的函数来查看当前的请求URL,(HttpContext.Current.Request.URL)。可以在快速监听里面进行查看。看一个网页总共请求了几次URL。在这里可以屏蔽IP,防盗猎图片等功能。

            protected void Application_BeginRequest(object sender,EventArgs e)
            {
                //实现功能,屏蔽IP
                if (HttpContext.Current.Request.UserHostAddress== "192.168.1.102")
                {
                    HttpContext.Current.Response.Write("这里就可以把自己电脑的IP地址屏蔽掉了。");               
                }
                else
                {
                    HttpContext.Current.Response.Write("");
                }
                //防盗猎
            }

    *:这个函数我也不是很清楚,等以后学习的时候在补吧。

            protected  void Application_AuthentiateRequest(object sender,EventArgs e)
            {
    
            }
    
    
  • 相关阅读:
    洛谷单元最短路标准版——spfa优化
    airline
    有依赖的背包
    挖地雷
    带分数问题
    子集和问题
    循环比赛日程表
    传纸条
    机器分配
    分组背包
  • 原文地址:https://www.cnblogs.com/netxiaohui/p/5092670.html
Copyright © 2020-2023  润新知