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

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

  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. }

我们来看这段:

  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类

  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

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

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页面

Asp.Net HttpContext.RemapHandler 用法的更多相关文章

  1. ASP.NET MVC HtmlHelper用法集锦

    ASP.NET MVC HtmlHelper用法集锦 在写一个编辑数据的页面时,我们通常会写如下代码 1:<inputtype="text"value='<%=View ...

  2. Asp.Net Server.MapPath()用法

    做了一个上传文件的功能 本地测试没问题 部署到服务器之后 一直报错 由于 某些历史原因 看不到错误信息 最后发现是路径的问题 其实这么简单的问题 最早该想到的 ...... Server.MapPat ...

  3. ASP.NET中EVAL用法大全

    <%# Bind("Subject") %> //绑定字段<%# Container.DataItemIndex + 1%> //实现自动编号<%# ...

  4. C# Eval在asp.net中的用法及作用

    Eval( " ")和Bind( " ") 这两种一个单向绑定,一个双向绑定,bind是双向绑定,但需数据源支持 ASP.NET 2.0改善了模板中的数据绑定操 ...

  5. ASP.NET HttpContext类

      IHttpHandler 接口 定义 ASP.NET 以异步方式处理使用自定义 HTTP 处理程序的 HTTP Web 请求而实现的协定 封装http请求信息 HttpContext.Curren ...

  6. [转]C# Eval在asp.net中的用法及作用

    原文链接:http://www.cnblogs.com/Mr_JinRui/archive/2010/07/06/1772129.html Eval( " ")和Bind( &qu ...

  7. ASP.NET MVC5----基本用法

    由于之前在项目中也使用MVC进行开发,但是具体是那个版本就不是很清楚了,但是我觉得大体的思想是相同的,只是版本高的在版本低的基础上增加了一些更加方便操作的东西.下面是我学习ASP.NET MVC5高级 ...

  8. C# ASP.NET MVC HtmlHelper用法大全

    UrlHrlper 下面的两个地址一样的功能 下边这个防止路由规则改变 比如UserInfo/Index改为UserInfo-Index,使用下面的不受影响 另一种形式的超链接: <%: Htm ...

  9. asp.net Gridview 的用法

    留个档. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="Fa ...

随机推荐

  1. 多点触摸(MT)协议(翻译)

    参考: http://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt 转自:http://www.arm9home.ne ...

  2. Redis学习笔记2-Redis的安装体验

    Redis的官方只提供了Linux版本的,并没提供Windows版本的(不过非官方有windows版本的.可以下载下来做开发测试学习用非常方便.博客后面会介绍到的).Linux下安装过程如下[以下命令 ...

  3. getSupportFragmentManager要用在FragmentActivity及其子类中

    getSupportFragmentManager要用在FragmentActivity及其子类中!! 关于安卓抽屉导航!! * 自定义侧边栏

  4. WIN32下使用DirectSound接口的简单音频播放器(支持wav和mp3)

    刚好最近接触了一些DirectSound,就写了一个小程序练练手,可以用来添加播放基本的wav和mp3音频文件的播放器.界面只是简单的GDI,dxsdk只使用了DirectSound8相关的接口. D ...

  5. delphi WebBrowser控件上网页验证码图片识别教程(一)

    步骤一:获取网页中验证码图片的url地址 在delphi中加入一个BitBtn和一个memo以及WebBrowser控件实现网页中验证码图片的url地址的获取 程序如下:procedure TForm ...

  6. 【WPF系列】基础 PasswordBox

    参考 How to bind to a PasswordBox in MVVM

  7. 在 KVM 上安装 Win7 虚拟机

    之前都是在用Linux 虚机,现在有需要用到Win7 虚机,才发现在 KVM 上安装 Win7 的过程远比想象中的复杂.本文就把其过程做个简单总结. 1. 在 Virtual Machine Mana ...

  8. Bootstrap 按钮

    本章将通过实例讲解如何使用 Bootstrap 按钮.任何带有 class .btn 的元素都会继承圆角灰色按钮的默认外观.但是 Bootstrap 提供了一些选项来定义按钮的样式,具体如下表所示: ...

  9. BZOJ1497: [NOI2006]最大获利[最小割 最大闭合子图]

    1497: [NOI2006]最大获利 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 4375  Solved: 2142[Submit][Status] ...

  10. HDU3466 Proud Merchants[背包DP 条件限制]

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...