图1:HttpModule生命周期示意图
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace MyHttpModule
{
public class ValidaterHttpModule : IHttpModule
{
#region IHttpModule 成员
public void Dispose()
{}
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(application_BeginRequest);
application.EndRequest += new EventHandler(application_EndRequest);
application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
application.PostRequestHandlerExecute += new EventHandler(application_PostRequestHandlerExecute);
application.ReleaseRequestState += new EventHandler(application_ReleaseRequestState);
application.AcquireRequestState += new EventHandler(application_AcquireRequestState);
application.AuthenticateRequest += new EventHandler(application_AuthenticateRequest);
application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);
application.ResolveRequestCache += new EventHandler(application_ResolveRequestCache);
application.PreSendRequestHeaders += new EventHandler(application_PreSendRequestHeaders);
application.PreSendRequestContent += new EventHandler(application_PreSendRequestContent);
}
void application_PreSendRequestContent(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_PreSendRequestContent<br/>");
}
void application_PreSendRequestHeaders(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_PreSendRequestHeaders<br/>");
}
void application_ResolveRequestCache(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_ResolveRequestCache<br/>");
}
void application_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_AuthorizeRequest<br/>");
}
void application_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_AuthenticateRequest<br/>");
}
void application_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_AcquireRequestState<br/>");
}
void application_ReleaseRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_ReleaseRequestState<br/>");
}
void application_PostRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_PostRequestHandlerExecute<br/>");
}
void application_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_PreRequestHandlerExecute<br/>");
}
void application_EndRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_EndRequest<br/>");
}
void application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.Context.Response.Write("application_BeginRequest<br/>");
}
#endregion
}
}
多个自定义的Http Module的运作
在HttpModule中终止此次的HTTP请求
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace MyHttpModule
{
public class CompleteRequestHttpModule : IHttpModule
{
#region IHttpModule 成员
public void Dispose()
{}
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(Application_BeginRequest);
}
void Application_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
application.CompleteRequest();
application.Context.Response.Write("请求被终止。");
}
#endregion
}
}
//==============================入门代码=========================================//
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace James.HttpModules
...{
public class HttpModulesDemo:IHttpModule
...{
public void Dispose()
...{
}
public void Init(HttpApplication context)
...{
context.EndRequest+= new EventHandler(context_EndRequest);
}
public void context_EndRequest(object sender, EventArgs e)
...{
HttpApplication httpApp = (HttpApplication)sender;
httpApp.Response.Output.Write("<br />©2007 thinkCode FooterModule");
}
}
}
在web.config 里面配置
<configuration>
<system.web>
<httpModules>
<add name="CopyRight" type="James.HttpModules.HttpModulesDemo, James.HttpModules"/>
</httpModules>
</system.web>
</configuration>
James.HttpModules—命名空间
James.HttpModules.HttpModulesDemo 类
//httphandle and httpmodule
ASP.Net处理Http Request时,使用Pipeline(管道)方式,由各个HttpModule对请求进行处理,然后到达HttpHandler,HttpHandler处理完之后,仍经过Pipeline中各个HttpModule的处理,最后将HTML发送到客户端浏览器中。ASP.Net系统本身配置有很多HttpHandler和HttpModule,以处理aspx等.Net标准的页面文件,以及这些页面文件中标准的事件处理等。查看%System%/Microsoft.NET/Framework/v2.0.50727/CONFIG目录下的web.config文件中的httpHandlers和httpModules节点,可以看到这些配置。如果有兴趣,可以使用Reflector查看.Net系统中相关的类和方法,了解.Net如何处理以及做了什么处理。.Net也提供了一套机制来开发自定义的HttpHandler和HttpModule,均可以用于对HttpRequest的截取,完成自定义的处理。 HttpModule 继承System.Web.IHttpModule接口,实现自己的HttpModule类。必须要实现接口的两个方法:Init和Dispose。在Init中,可以添加需要截取的事件;Dispose用于资源的释放,如果在Init中创建了自己的资源对象,请在Dispose中进行释放。 namespace MyModule
{
public class MyHttpModule : IHttpModule
{
public MyHttpModule()
{
} public void Init(HttpApplication r_objApplication)
{
r_objApplication.BeginRequest += new EventHandler(this.BeginRequest);
} public void Dispose()
{
} private void BeginRequest(object r_objSender, EventArgs r_objEventArgs)
{
HttpApplication objApp = (HttpApplication)r_objSender;
objApp.Response.Write("您请求的URL为" + objApp.Request.Path);
}
}
} 将编译的dll文件拷贝到web项目的bin目录下,在web项目的web.config文件system.web节点中配置:
这样就将自定义的HttpModule类MyHttpModule插入到了当前web的HttpModule的Pipeline中。 HttpModule主要功能是对Application的各个事件进行截取,在这些事件中完成自己的处理。其实如果自己开发一些项目,直接在Global.asax中处理已经足够了。如果是开发一个Framework或者是某些方面的组件,需要在事件中添加处理,开发自定义的HttpModule,可以避免使用Framework或者组件时,还得手工在Global.asax中添加代码。 目前想到的开发自定义HttpModule的用途,有全局的身份/权限验证、自定义网站访问/操作日志的记录、处于管理/调试等目的对站点进行监控追踪等。当然,如果是结合自定义的HttpHandler进行Framework的开发,HttpModule可以用于其它的一些特殊的处理。 HttpHandler是完全的对Http Request的截取。
首先,继承System.Web.IHttpHandler接口,实现自己的HttpHandler类。必须要实现接口的ProcessRequest方法和IsReusable属性。ProcessRequest方法中完成对每个Http Request的处理,发送处理结果的HTML到输出缓存中。IsReusable属性被.Net Framework调用,用以确定这个HttpHandler的实例是否可以被重用于同类型其它的Request处理。
如果你在自己的HttpHandler类中,需要读取或者是写Session值,需要再继承一个接口IRequiresSessionState。这个接口没有任何方法,只是一个标记接口。继承这个接口之后,就可以在自己的HttpHandler中访问Session,可以在Session中写入值。
namespace MyHandler
{
public class MyHttpHandler : IHttpHandler, IRequiresSessionState
{
public MyHttpHandler() {}
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
HttpResponse objResponse = context.Response ;
objResponse.Write("
This request is handled by MyHttpHandler
");
}
}
}
把编译的dll文件拷贝到web项目的bin目录下。
接下来,这样来测试一下MyHttpHandler。我们为IIS配置一个以.cc为后缀名的文件类型,用我们写的MyHttpHandler来处理。
首先,在IIS站点的Configuration配置里面,添加一个对.cc后缀名处理的Application Extention Mapping项。
然后,在web项目的web.config节点节点中配置:
MyHttpHandler, MyHandler"/>
verb属性配置这个HttpHandler处理那些HTTP方法,例如GET、POST等,如果是处理所有方法,就用*。path属性配置HttpHandler对哪些文件进行处理,例如可以是myfile.cc,如果是处理所有的.cc文件,就用*.cc。
这样,这个站点上所有.cc类型文件的访问,都由MyHttpHandler处理。使用http://localhost/站点虚拟目录/a.cc访问测试站点,可以看到测试效果。当然,a.cc这个文件在Web服务器上是并不存在的。
对HttpHandler的使用,比较典型的有.Net的Web MVC开源项目Maverick。Maverick使用一个Dispatcher类对所有的Http Request进行截取,他以.m作为后缀名向Web服务器提交请求,在Dispatcher中,将.m的后缀去掉,提取Command Name,然后以这个command name从配置文件中加载处理的flow,形成一个chain,依次对chain上的各个command和view进行处理,对各个command和view的处理结果可能会在chain中选择不同的处理分支,每个处理的Step中将处理结果的HTML写入Response的缓存中进行输出。
总体来说,Maverick的框架架构概念很不错,但也存在明显的缺陷,以后有时间再详细的写写它的架构和需要改进之处。
总之,将HttpModule、HttpHandler,以及使用Ajax等将客户端进行封装结合起来,能够给web项目的开发带来非常大的改善空间。
Asp.Net HttpHandler实现URL重写的
我们经常看到很多网站访问文章的时候才用的是***.html或***.shtml (如本blog的日志访问效果),其时这写文件在服务器上不存在的,那为什么会出现这样的效果呢,是因为Web服务器上对URL执行了重写,把访问的URL根据特定的格式重写成内部访问页面来实现的,它的好处是便于用户理解,同时搜索引擎也能更好地收入你的网站,当然其它的好处也很多,这里不做一一介绍了。
本文所讲的是使用Asp.Net中的HttpHandler实现URL重写的,它所实现的原理请看这里,本程序可以处理任何Url,因为我在程序中使用了URL过虑,只有访问文件名是数字的才进行处理,并指在内部执行一个新的页面,并输出数据,代码如下:
public void ProcessRequest(HttpContext Context)
{
try {
//申明Request
HttpRequest Request = Context.Request;
//取来路Url的绝对路径
string Url = Request.Url.AbsolutePath;
//取访问的Web文件的开始字符间隔数
int RegStart = Url.LastIndexOf("/") + 1;
//申明一个确定Web文件名是否全是数字
Regex Reg = new Regex(@"/d+");
//用正则表达式进行匹配
if (Reg.IsMatch(Url, RegStart))
{
//如果web文件名是数字,则判定是查询相关文章,执行指定页面 Context.Server.Execute("~/PermaLink.aspx?id=" + Reg.Match(Url, RegStart).Value);
}
}
catch
{
Context.Response.Redirect(Context.Request.Url.ToString());
}
}
当然你首先要做的是先建一个类,并继承自IHttpHandler,然后把这段代码拷入,并编译。在Web项目中若要使用此功能,需要在web.config里面加上如下语句:
<httpHandlers>
<add verb="*" path="*.shtml" type="HttpHandle.UrlRewrite" />
</httpHandlers>
同时,还要在IIS中对Web项目进行配置,在Web项目的属性中,在主目录选项卡里,把执行权限改为"脚本和可执行文件",然后打开配置,在应用程序扩展里加上需重写的文件格式的扩展,好了,成事具备,只欠运行了。
//=======================================================//
<system.web>
<httpModules>
<add name="UrlRewrite" type="UrlRewrite.Rewrite, UrlRewrite" />
</httpModules>
</system.web>
Rewrite.cs:
using System;
using System.Web;
namespace UrlRewrite
{
/** <summary>
/// Rewrite 的摘要说明。
/// </summary>
public class Rewrite:System.Web.IHttpModule
{
public Rewrite()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
IHttpModule 成员#region IHttpModule 成员
public void Init(HttpApplication context) {
// TODO: 添加 Rewrite.Init 实现
context.BeginRequest +=new EventHandler(context_BeginRequest);
}
public void Dispose() {
// TODO: 添加 Rewrite.Dispose 实现
}
#endregion
private void context_BeginRequest(object sender, EventArgs e) {
HttpApplication app = (HttpApplication)sender;
app.Context.RewritePath("your url");//aa.aspx
}
}
}
Asp.net Url重写(HttpModule)
web.config: