Global.asax 文件
在上一节中写道。HttpApplieaction 类,将通过创建 HttpApplication 类的实例启动应用程序,第一次在应用程序中请求
ASP.NET 页或进程时,将创建 HttpApplication 的一个新实例。不过,为了尽可能提高性能,可对多个请求重复使用HttpApplication 实例。而当我们创建Global.asax后,ASP.NET
会将其编译为从 HttpApplication 类派生的类,然后使用该派生类表示应用程序。更多请看:http://blog.youkuaiyun.com/shuanger_/article/details/41322143
注意:Global.asax一个应用程序最多有一个。可以没有,没有的话,他自己调用HttpApplication。既然是继承,那么他大部分其实是对HttpApplication的重写,重写方法有如下:
Applicaton_init() :第一次请求该应用程序的时候,HttpApplication被实例化的时候调用。
Applicaton_start()
:init实例化的时候调用,允许你在该方法中编写HttpApplication访问的全局变量。或者调用你想第一访问的方法。
Applicaton_BeginRequest():像Web发送请求的时候,被第一个调用。可以进行seesion验证等。
Applicaton_EndRequest()
:在应用程序对该请求执行完成之后。调用
Applicaton_PreSendHeader()
: 应用程序向浏览器发送HTTP头的时候被调用
Applicaton_PreSendContext()
: 应用程序向浏览器发送HTTP内容的时候被调用
Applicaton_Re():
Application_updateRequestCache():应用程序完成请求后,更新内存,一遍后续相应的时候,直接调用内存数据。这就是为什么第一次响应很快,但是后面的响应就变慢了
Session_Strat():一个新用户访问web的时候
Session_End():Session
超时的时候被调用
在此就列举一些,想了解更多MSCD等着你。
以下是一段对全局变量,报错信息,认证的代码。这比写在Control中要方便一大截。
protected
void Application_Start(Object sender, EventArgs e) {
Application["Title"] = "Builder.com Sample";
}
protected void Session_Start(Object sender, EventArgs e) {
Session["startValue"] = 0;
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e) {
// Extract the forms authentication cookie
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if(null == authCookie) {
// There is no authentication cookie.
return;
}
FormsAuthenticationTicket authTicket = null;
try {
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
} catch(Exception ex) {
// Log exception details (omitted for simplicity)
return;
}
if (null == authTicket) {
// Cookie failed to decrypt.
return;
}
// When the ticket was created, the UserData property was assigned
// a pipe delimited string of role names.
string[2] roles
roles[0] = "One"
roles[1] = "Two"
// Create an Identity object
FormsIdentity id = new FormsIdentity( authTicket );
// This principal will flow throughout the request.
GenericPrincipal principal = new GenericPrincipal(id, roles);
// Attach the new principal object to the current HttpContext object
Context.User = principal;
}
protected void Application_Error(Object sender, EventArgs e) {
Response.Write("Error encountered.");
}
Application["Title"] = "Builder.com Sample";
}
protected void Session_Start(Object sender, EventArgs e) {
Session["startValue"] = 0;
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e) {
// Extract the forms authentication cookie
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if(null == authCookie) {
// There is no authentication cookie.
return;
}
FormsAuthenticationTicket authTicket = null;
try {
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
} catch(Exception ex) {
// Log exception details (omitted for simplicity)
return;
}
if (null == authTicket) {
// Cookie failed to decrypt.
return;
}
// When the ticket was created, the UserData property was assigned
// a pipe delimited string of role names.
string[2] roles
roles[0] = "One"
roles[1] = "Two"
// Create an Identity object
FormsIdentity id = new FormsIdentity( authTicket );
// This principal will flow throughout the request.
GenericPrincipal principal = new GenericPrincipal(id, roles);
// Attach the new principal object to the current HttpContext object
Context.User = principal;
}
protected void Application_Error(Object sender, EventArgs e) {
Response.Write("Error encountered.");
}
本文详细介绍了ASP.NET中的Global.asax文件及其工作原理。包括HttpApplication类的实例化过程,以及Global.asax中常用事件的应用,如初始化、开始请求、结束请求等。此外,还提供了示例代码展示如何设置全局变量、处理认证及错误。
1171

被折叠的 条评论
为什么被折叠?



