鲜荣彬
Herry

 

   在前面的博文中 我阐述了自己对页面生命周期的一些理解,我觉得自己可能还没有阐述清楚,因此我在这里模拟 Http 请求处理管道的一些过程,其实这些事件在 Global.asax 中是有体现的,我只是在这里将其细化,供大家更好的理解Http 请求处理管道的请求过程,以便在asp.net 应用处理程序完成自己的操作。

Global.asax  中 有这样几个事件 

   

复制代码
View Code
   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 相差无几!!

复制代码
View Code
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("处理前...");
}
}
复制代码


   

复制代码
View Code
 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); }
        }

       

复制代码
View Code
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();
}
}

}
复制代码

 

  

posted on   Herry彬  阅读(344)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· MQ 如何保证数据一致性?
· 一个基于 .NET 开源免费的异地组网和内网穿透工具

< 2011年11月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1 2 3
4 5 6 7 8 9 10
点击右上角即可分享
微信分享提示