在前面的博文中 我阐述了自己对页面生命周期的一些理解,我觉得自己可能还没有阐述清楚,因此我在这里模拟 Http 请求处理管道的一些过程,其实这些事件在 Global.asax 中是有体现的,我只是在这里将其细化,供大家更好的理解Http 请求处理管道的请求过程,以便在asp.net 应用处理程序完成自己的操作。
在 Global.asax 中 有这样几个事件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Global : System.Web.HttpApplication {
protected void Application_Start(object sender, EventArgs e) { }
protected void Session_Start(object sender, EventArgs e) { }
protected void Application_BeginRequest(object sender, EventArgs e) { }
protected void Application_AuthenticateRequest(object sender, EventArgs e){ }
protected void Application_Error(object sender, EventArgs e) { }
protected void Session_End(object sender, EventArgs e){ }
protected void Application_End(object sender, EventArgs e) { } }
在几个事件中,我们就可以做我们自己想做的事情,比如对URL重写,就可以在 Application_BeginRequest 这个事件中处理,权限验证也可以在Application_AuthenticateRequest事件中处理,因此,了解页面生命周期可以帮助我们将应用程序写的更加完美,如同一位诗人写出美丽的诗句,是一样的道理。
这是我参考别人的代码写出的对Http请求处理管道的模拟,其实大致和 全局处理程序 Global.asax 相差无几!!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Program : System.ComponentModel.Component
{
static void Main(string[] args)
{
ProcessPripeline process = new ProcessPripeline();
process.StartProcess += new EventHandler(process_StartProcess);
process.PreProcess += new EventHandler(process_PreProcess);
process.PostProcess += new EventHandler(process_PostProcess);
process.EndProcess += new EventHandler(process_EndProcess);
process.Process();
}
static void process_StartProcess(object sender, EventArgs e)
{
Console.WriteLine("开始处理的事件处理...");
}
static void process_EndProcess(object sender, EventArgs e)
{
Console.WriteLine("处理完成后的事件处理...");
}
static void process_PostProcess(object sender, EventArgs e)
{
Console.WriteLine("处理后的事件处理...");
}
static void process_PreProcess(object sender, EventArgs e)
{
Console.WriteLine("处理前...");
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class ProcessPripeline : System.ComponentModel.Component //提供 IComponent 接口的基实现并启用应用程序之间的对象共享。
{
private static readonly object startEvent = new object();
private static readonly object prpeProcessEvent = new object();
private static readonly object postProcessEvent = new object();
private static readonly object endEvent = new object();
public event EventHandler StartProcess//表示将处理不包含事件数据的事件的方法。
{
add {this.Events.AddHandler(startEvent,value); }
remove { this.Events.RemoveHandler(startEvent, value); }
}
public event EventHandler PreProcess
{
add { this.Events.AddHandler(prpeProcessEvent, value); }
remove { this.Events.RemoveHandler(prpeProcessEvent, value); }
}
public event EventHandler PostProcess
{
add { this.Events.AddHandler(postProcessEvent, value); }
remove { this.Events.RemoveHandler(postProcessEvent, value); }
}
public event EventHandler EndProcess
{
add { this.Events.AddHandler(endEvent, value); }
remove { this.Events.RemoveHandler(endEvent, value); }
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void OnStartProcess(EventArgs e)
{
if (this.Events[startEvent] != null)
{
(this.Events[startEvent] as EventHandler)(this, e);
}
}
protected void OnpreProcess(EventArgs e)
{
if (this.Events[prpeProcessEvent] != null)
{
(this.Events[prpeProcessEvent] as EventHandler)(this, e);
}
}
protected void OnPostProcess(EventArgs e)
{
if (this.Events[postProcessEvent] != null)
{
(this.Events[postProcessEvent] as EventHandler)(this, e);
}
}
protected void OnEndProcess(EventArgs e)
{
if (this.Events[endEvent] != null)
{
(this.Events[endEvent] as EventHandler)(this, e);
}
}
public void Process()
{
Console.WriteLine("start...");
this.OnStartProcess(EventArgs.Empty);
Console.WriteLine("begin handle...");
this.OnpreProcess(EventArgs.Empty);
Console.WriteLine("handling...");
Console.WriteLine("handle ok");
this.OnPostProcess(EventArgs.Empty);
Console.WriteLine("ok");
this.OnEndProcess(EventArgs.Empty);
Console.Read();
}
}
}