HttpHandler实现了ISAPI Extention的功能,他处理请求(Request)的信息和发送响应(Response)HttpHandler功能的实现通过实现IHttpHandler接口来达到。
看图先:
在ASP.NET管道处理的末端是HTTP Hander,其实每个Asp.net的Page都实现了IHttpHander,在VS.net中的对象察看器中你可以证实这一点:
接口IHttpHandler的定义如下:
interface IHttpHandler{ void ProcessRequest(HttpContext ctx); bool IsReuseable { get; }}
接口中ProcessRequest是添加自己的代码进行相应处理的地方。IsReuseable属性指明该HttpHandler的实现类是否需要缓存。
例1:
using System;using System.Web;namespace HttpHandlerTest{ public class Redirect : IHttpHandler { public Redirect() { } public void ProcessRequest(HttpContext context) { HttpServerUtility Server = context.Server; Server.Transfer("test1.aspx"); } public bool IsReusable { get { return false; } } }}
这里的Redirect功能是在web请求满足HttpHandler配置文件中预设条件下自动跳转到另一个页面,在web.config中<httpHandlers>节点配置如下:
<add verb="*" path="test.aspx" type="HttpHandlerTest.Redirect, HttpHandlerTest" />