转自http://ugoer.cnblogs.com/archive/2005/09/07/231676.html
HttpHandler实现了ISAPI Extention的功能,他处理请求(Request)的信息和发送响应(Response)。HttpHandler功能的实现通过实现IHttpHandler接口来达到。
看图先:
<shapetype stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600" id="_x0000_t75"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype>
在ASP.NET 管道处理的末端是HTTP Hander,其实每个Asp.net的Page都实现了IHttpHander,在VS.net中的对象察看器中你可以证实这一点
具体的类是这样定义的:public class Page : TemplateControl, IhttpHandler。
接口IHttpHandler的定义如下:
interface
IHttpHandler
{
voidProcessRequest(HttpContextctx);
boolIsReuseable{get;}
}
接口中
ProcessRequest
是添加自己的代码进行相应处理的地方。
IsReuseable
属性指明该
HttpHandler
的实现类是否需要缓存。
在CS中有很多继承IHttpHandler接口的类,我取出有代表性而又容易理解的一个做分析:找到CommunityServerComponents项目Components目录下的Redirect.cs文件,内容如下:
//
------------------------------------------------------------------------------
//
<copyrightcompany="TelligentSystems">
//
Copyright(c)TelligentSystemsCorporation.Allrightsreserved.
//
</copyright>
//
------------------------------------------------------------------------------
using
System;
using
System.Web;
namespace
CommunityServer.Components
{
/**////<summary>
///SummarydescriptionforRedirect.
///</summary>
publicclassRedirect:IHttpHandler
{
publicRedirect()
{
//
//TODO:Addconstructorlogichere
//
}
publicvoidProcessRequest(HttpContextcontext)
{
stringurl=context.Request.QueryString["u"];
if(!Globals.IsNullorEmpty(url))
{
context.Response.Redirect(url);
}
else
{
context.Response.Redirect(Globals.GetSiteUrls().Home);
}
context.Response.End();
}
publicboolIsReusable
{
get{returnfalse;}
}
}
}
这里的Redirect功能是在web请求满足HttpHandler配置文件中预设条件下自动拦截执行的,在web.config中<httpHandlers>节点下可以看到
<
add
verb
="GET"
path
="Utility/redirect.aspx"
type
="CommunityServer.Components.Redirect,CommunityServer.Components"
/>
对该类的配置
· verb可以是"GET"或"POST",表示对GET或POST的请求进行处理。"*"表示对所有请求进行处理,这里是对GET请求进行处理。
· path指明对相应的文件进行处理,"*.aspx"表示对发给所有ASPX页面的请求进行处理,这里单独对redirect.aspx页面进行处理。可以指明路径,如"blogs"。表明只对blogs目录下的redirect.aspx文件请求进行处理。
· type属性中,逗号前的字符串指明HttpHandler的实现类的类名,后面的字符串指明Dll文件的名称。
实际处理是怎么样的呢?其实redirect.aspx页面在CS项目中并不存在,CS把对redirect.aspx的请求处理交给了CommunityServer.Components.dll程序集中Redirect类进行处理。处理的过程是执行
public void ProcessRequest(HttpContext context)
方法(Redirect类下的ProcessRequest方法是对当前请求的上下文Context中Url是否包含“u”参数,如果有并且参数值不为null就调用Response.Redirect方法跳转到“u”参数值所执行的页面,如果没有参数或者参数值为空就跳转到CS的首页)。
另外在CS中对RSS和Trackback的处理都使用了httpHandler的处理方式。
前面提到,所有页面的基类Page都实现了HttpHandler接口,因此每个asp.net的页面都可以看成是一个HttpHandler处理类,只是配置部分在machine.config中
<
httpHandlers
>
<
add
verb
="*"
path
="*.vjsproj"
type
="System.Web.HttpForbiddenHandler"
/><
add
verb
="*"
path
="*.java"
type
="System.Web.HttpForbiddenHandler"
/><
add
verb
="*"
path
="*.jsl"
type
="System.Web.HttpForbiddenHandler"
/><
add
verb
="*"
path
="trace.axd"
type
="System.Web.Handlers.TraceHandler"
/>
<
add
verb
="*"
path
="*.aspx"
type
="System.Web.UI.PageHandlerFactory"
/>
<
add
verb
="*"
path
="*.ashx"
type
="System.Web.UI.SimpleHandlerFactory"
/>
<
add
verb
="*"
path
="*.asmx"
type
="System.Web.Services.Protocols.WebServiceHandlerFactory,System.Web.Services,Version=1.0.5000.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"
validate
="false"
/>
<
add
verb
="*"
path
="*.rem"
type
="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory,System.Runtime.Remoting,Version=1.0.5000.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"
validate
="false"
/>
<
add
verb
="*"
path
="*.soap"
type
="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory,System.Runtime.Remoting,Version=1.0.5000.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"
validate
="false"
/>
<
add
verb
="*"
path
="*.asax"
type
="System.Web.HttpForbiddenHandler"
/>
<
add
verb
="*"
path
="*.ascx"
type
="System.Web.HttpForbiddenHandler"
/>
<
add
verb
="GET,HEAD"
path
="*.dll.config"
type
="System.Web.StaticFileHandler"
/>
<
add
verb
="GET,HEAD"
path
="*.exe.config"
type
="System.Web.StaticFileHandler"
/>
<
add
verb
="*"
path
="*.config"
type
="System.Web.HttpForbiddenHandler"
/>
<
add
verb
="*"
path
="*.cs"
type
="System.Web.HttpForbiddenHandler"
/>
<
add
verb
="*"
path
="*.csproj"
type
="System.Web.HttpForbiddenHandler"
/>
<
add
verb
="*"
path
="*.vb"
type
="System.Web.HttpForbiddenHandler"
/>
<
add
verb
="*"
path
="*.vbproj"
type
="System.Web.HttpForbiddenHandler"
/>
<
add
verb
="*"
path
="*.webinfo"
type
="System.Web.HttpForbiddenHandler"
/>
<
add
verb
="*"
path
="*.asp"
type
="System.Web.HttpForbiddenHandler"
/>
<
add
verb
="*"
path
="*.licx"
type
="System.Web.HttpForbiddenHandler"
/>
<
add
verb
="*"
path
="*.resx"
type
="System.Web.HttpForbiddenHandler"
/>
<
add
verb
="*"
path
="*.resources"
type
="System.Web.HttpForbiddenHandler"
/>
<
add
verb
="GET,HEAD"
path
="*"
type
="System.Web.StaticFileHandler"
/>
<
add
verb
="*"
path
="*"
type
="System.Web.HttpMethodNotAllowedHandler"
/>
</
httpHandlers
>
借助一个工具:Reflector,看看Page类下的HttpHandler处理方法ProcessRequest都做了什么
[EditorBrowsable(EditorBrowsableState.Never)]
public
void
ProcessRequest(HttpContextcontext)
{
this.SetIntrinsics(context);
this.ProcessRequest();
}
private
void
SetIntrinsics(HttpContextcontext)
{
this._context=context;
this._request=context.Request;
this._response=context.Response;
this._application=context.Application;
this._cache=context.Cache;
if((this._clientTarget!=null)&&(this._clientTarget.Length>0))
{
this._request.ClientTarget=this._clientTarget;
}
base.HookUpAutomaticHandlers();
}
private
void
ProcessRequest()
{
Threadthread1=Thread.CurrentThread;
CultureInfoinfo1=thread1.CurrentCulture;
CultureInfoinfo2=thread1.CurrentUICulture;
this.FrameworkInitialize();
try
{
try
{
if(this.IsTransacted)
{
this.ProcessRequestTransacted();
}
else
{
this.ProcessRequestMain();
}
this.ProcessRequestEndTrace();
}
finally
{
this.ProcessRequestCleanup();
InternalSecurityPermissions.ControlThread.Assert();
thread1.CurrentCulture=info1;
thread1.CurrentUICulture=info2;
}
}
catch
{
throw;
}
}
在Page类的ProcessRequest方法先是把上下文Context内容赋值到当前Page中的一些属性里,然后调用System.Web.UI.TemplateControl中的HookUpAutomaticHandlers()
internal
void
HookUpAutomaticHandlers()
{
if(this.SupportAutoEvents)
{
SimpleBitVector32vector1=newSimpleBitVector32(this.AutoHandlers);
InternalSecurityPermissions.Reflection.Assert();
if(!vector1[1])
{
vector1[1]=true;
this.GetDelegateInformation("Page_Init",refvector1,2,4);
this.GetDelegateInformation("Page_Load",refvector1,8,0x10);
this.GetDelegateInformation("Page_DataBind",refvector1,0x20,0x40);
this.GetDelegateInformation("Page_PreRender",refvector1,0x80,0x100);
this.GetDelegateInformation("Page_Unload",refvector1,0x200,0x400);
this.GetDelegateInformation("Page_Error",refvector1,0x800,0x1000);
http://ugoer.cnblogs.c
分享到:
评论