Global.asax文件的作用:
1.定义Application级别,Session级别的变量,对象和数据;
2.定义在Application中的,基于Application,Session的事件;
定义Application,Session级别的变量:
使用Application对象来定义Application范围的变量,使用Session对象定义Session范围的变量。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
PipeLine指定了标示的对象或者组件的作用范围是当前的HttpApplication实例,并且不是共享的。
Application事件:
Application_Start— Application开始时触发;
Application_End— Application停止时触发;
Application_BeginRequest— 收到Request触发
Application_EndRequest— 结束Request触发
Application_AuthenticationRequest— 收到鉴权请求触发
Application_Error— 错误时触发
Session事件:
Session_Start— 客户端建立Session时触发;
Session_End— 客户端结束Session时触发;
脚本依然允许在global.asax中使用,可以定义变量,事件;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
当改动global.asax文件时,Asp.Net 会检测到这个文件的改变,将处理完现有的所有Request后,触发
Application_OnEnd()事件,然后重新启动Application。
上面的处理过程,会重置整个站点,清除Session和其它存储的信息;当有新的Request到达时,Asp.Net 会重新编译global.asax文件,触发Application_OnEnd()事件。
所以,如果要更改global.asax文件,应该在站点不繁忙时做。
关于global.asax文件要注意的几点:
global.asax文件存在于站点的根目录;
每个站点只允许有1个global.asax文件;
当对站点资源的第一次请求时,asp.net 会将global.asax文件编译为一个类(继承自HttpApplication类);
global.asax文件是自配置的,处于安全目的,不允许外部通过Url访问;
参考:
Inside Asp.Net
Scott Worley
Publisher: New Riders Publishing
First Edition November 15, 2001
ISBN: 0-7357-1135-6, 730 pages
作者:Angelo Lee
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.