从请求管道深入剖析HttpModule的实现机制图文介绍(2)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HttpApplicationProcessMethodDemo
{
class Program
{
static void Main(string[] args)
{
//ISAPIRuntime
//假设根据请求创建了HttpContext上下文
HttpContext context = new HttpContext();
//假设从HttpApplicationFactory创建出来
HttpApplication application = new HttpApplication();
//把所有注册在配置文件中的HttpModule加载并执行其Init方法
application.InitInternal();
//调用pr方法开始执行页面的pr方法
application.ProcessRequest(context);
Console.ReadKey();
}
}
}


6.自定义一个HttpModule

复制代码 代码如下:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HttpApplicationProcessMethodDemo
{
/// <summary>
/// 自定义的HttpModule模块
/// </summary>
public class MyHttpModule : IHttpModule
{
/// <summary>
/// 实现了IHttpModule接口
/// </summary>
/// <param></param>
public void Init(HttpApplication application)
{
//注册事件
application.BeginRequest += new EventHandler(application_BeginRequest);
application.PostResolveRequestCache += new EventHandler(application_PostResolveRequestCache);
application.EndRequest += new EventHandler(application_EndRequest);
}
void application_EndRequest(object sender, EventArgs e)
{
Console.WriteLine("HttpModule注册了EndRequest方法");
}
void application_PostResolveRequestCache(object sender, EventArgs e)
{
Console.WriteLine("HttpModule注册了PostResolveRequestCache方法");
}
void application_BeginRequest(object sender, EventArgs e)
{
Console.WriteLine("HttpModule注册了BeginRequest方法");
}
}
}


通过以上的步骤就实现了整个管道的执行过程和HttpModule的实现原理了。
下面是效果图

从请求管道深入剖析HttpModule的实现机制图文介绍

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjdxxz.html