Asp.Net HttpContext.RemapHandler 用法

最近在看HttpHandler映射过程文章时发现Context对象中有一个RemapHandler方法,它能将当前请求映射到指定的HttpHandler处理,可跳过系统默认的Httphandler。它的好处是我们可以在代码中根据需求动态的指定HttpHandler。个人觉得挺好用,就分享给大家。

 

通过.net 反编译工具看到获取HttpHandler的方法,关键代码如下:

 

[csharp] view plain copy
 
  1. internal IHttpHandler MapHttpHandler(HttpContext context, string requestType,   
  2.                                     VirtualPath path, string pathTranslated, bool useAppConfig)  
  3. {  
  4.     IHttpHandler handler = (context.ServerExecuteDepth == 0) ? context.RemapHandlerInstance : null;  
  5.     using( new ApplicationImpersonationContext() ) {  
  6.         if( handler != null )  
  7.             return handler;  
  8.         HttpHandlerAction mapping = this.GetHandlerMapping(context, requestType, path, useAppConfig);  
  9.         if( mapping == null )   
  10.             throw new HttpException("Http_handler_not_found_for_request_type");  
  11.         IHttpHandlerFactory factory = this.GetFactory(mapping);  
  12.         IHttpHandlerFactory2 factory2 = factory as IHttpHandlerFactory2;  
  13.         if( factory2 != null )   
  14.             handler = factory2.GetHandler(context, requestType, path, pathTranslated);                  
  15.         else   
  16.             handler = factory.GetHandler(context, requestType, path.VirtualPathString, pathTranslated);  
  17.         this._handlerRecycleList.Add(new HandlerWithFactory(handler, factory));  
  18.     }  
  19.     return handler;  
  20. }  

 

 

我们来看这段:

 

[csharp] view plain copy
 
  1. IHttpHandler handler = (context.ServerExecuteDepth == 0) ? context.RemapHandlerInstance : null;  
  2.     using( new ApplicationImpersonationContext() ) {  
  3.         if( handler != null )//如果调用了HttpContext.RemapHttpHandler方法,则直接返回设置的HttpHandler对象,跳过后面获取系统默认的HttpHandler处理程序  
  4.             return handler;  

如果之前调用了HttpContext.RemapHttpHandler()方法,则返回Context.RemapHandlerInstance,即RemapHttpHandler方法指定的HttpHandler实例。

 

实例:

Global.asax中的Application_BeginRequest事件中指定自定义的TestHandler类

 

[csharp] view plain copy
 
  1. public class Global : System.Web.HttpApplication  
  2.     {  
  3.         protected void Application_BeginRequest(object sender, EventArgs e)  
  4.         {  
  5.             HttpApplication app = (HttpApplication)sender;  
  6.             app.Context.RemapHandler(new TestHandler());  
  7.         }  
  8.     }  



 

TestHandler.cs

 

[csharp] view plain copy
 
  1. public class TestHandler:IHttpHandler  
  2.     {  
  3.         public bool IsReusable  
  4.         {  
  5.             get { return true; }  
  6.         }  
  7.   
  8.         public void ProcessRequest(HttpContext context)  
  9.         {  
  10.             context.Response.Write("TestHandler");  
  11.         }  
  12.     }  


Default.aspx

 

 

 
  1. <body>  
  2.     <form id="form1" runat="server">  
  3.     <div>  
  4.     Default.aspx  
  5.     </div>  
  6.     </form>  
  7. </body>  



 

在网址中输入请求Default.aspx,响应结果:

可以看出,系统调用了自定义的TestHandler,没有执行Default.aspx页面

 

posted @ 2016-08-30 14:28  nele  阅读(340)  评论(0)    收藏  举报