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记录到日志中。spa
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(""); } //防盗猎 }
*:这个函数我也不是很清楚,等之后学习的时候在补吧。blog
protected void Application_AuthentiateRequest(object sender,EventArgs e) { }