Asp.Net HttpContext.RemapHandler 用法
最近在看HttpHandler映射过程文章时发现Context对象中有一个RemapHandler方法,它能将当前请求映射到指定的HttpHandler处理,可跳过系统默认的Httphandler。它的好处是我们可以在代码中根据需求动态的指定HttpHandler。个人觉得挺好用,就分享给大家。
通过.net 反编译工具看到获取HttpHandler的方法,关键代码如下:
- internal IHttpHandler MapHttpHandler(HttpContext context, string requestType,
- VirtualPath path, string pathTranslated, bool useAppConfig)
- {
- IHttpHandler handler = (context.ServerExecuteDepth == 0) ? context.RemapHandlerInstance : null;
- using( new ApplicationImpersonationContext() ) {
- if( handler != null )
- return handler;
- HttpHandlerAction mapping = this.GetHandlerMapping(context, requestType, path, useAppConfig);
- if( mapping == null )
- throw new HttpException("Http_handler_not_found_for_request_type");
- IHttpHandlerFactory factory = this.GetFactory(mapping);
- IHttpHandlerFactory2 factory2 = factory as IHttpHandlerFactory2;
- if( factory2 != null )
- handler = factory2.GetHandler(context, requestType, path, pathTranslated);
- else
- handler = factory.GetHandler(context, requestType, path.VirtualPathString, pathTranslated);
- this._handlerRecycleList.Add(new HandlerWithFactory(handler, factory));
- }
- return handler;
- }
我们来看这段:
- IHttpHandler handler = (context.ServerExecuteDepth == 0) ? context.RemapHandlerInstance : null;
- using( new ApplicationImpersonationContext() ) {
- if( handler != null )//如果调用了HttpContext.RemapHttpHandler方法,则直接返回设置的HttpHandler对象,跳过后面获取系统默认的HttpHandler处理程序
- return handler;
如果之前调用了HttpContext.RemapHttpHandler()方法,则返回Context.RemapHandlerInstance,即RemapHttpHandler方法指定的HttpHandler实例。
实例:
Global.asax中的Application_BeginRequest事件中指定自定义的TestHandler类
- public class Global : System.Web.HttpApplication
- {
- protected void Application_BeginRequest(object sender, EventArgs e)
- {
- HttpApplication app = (HttpApplication)sender;
- app.Context.RemapHandler(new TestHandler());
- }
- }
TestHandler.cs
- public class TestHandler:IHttpHandler
- {
- public bool IsReusable
- {
- get { return true; }
- }
- public void ProcessRequest(HttpContext context)
- {
- context.Response.Write("TestHandler");
- }
- }
Default.aspx
- <body>
- <form id="form1" runat="server">
- <div>
- Default.aspx
- </div>
- </form>
- </body>
在网址中输入请求Default.aspx,响应结果:
可以看出,系统调用了自定义的TestHandler,没有执行Default.aspx页面
Asp.Net HttpContext.RemapHandler 用法的更多相关文章
- ASP.NET MVC HtmlHelper用法集锦
ASP.NET MVC HtmlHelper用法集锦 在写一个编辑数据的页面时,我们通常会写如下代码 1:<inputtype="text"value='<%=View ...
- Asp.Net Server.MapPath()用法
做了一个上传文件的功能 本地测试没问题 部署到服务器之后 一直报错 由于 某些历史原因 看不到错误信息 最后发现是路径的问题 其实这么简单的问题 最早该想到的 ...... Server.MapPat ...
- ASP.NET中EVAL用法大全
<%# Bind("Subject") %> //绑定字段<%# Container.DataItemIndex + 1%> //实现自动编号<%# ...
- C# Eval在asp.net中的用法及作用
Eval( " ")和Bind( " ") 这两种一个单向绑定,一个双向绑定,bind是双向绑定,但需数据源支持 ASP.NET 2.0改善了模板中的数据绑定操 ...
- ASP.NET HttpContext类
IHttpHandler 接口 定义 ASP.NET 以异步方式处理使用自定义 HTTP 处理程序的 HTTP Web 请求而实现的协定 封装http请求信息 HttpContext.Curren ...
- [转]C# Eval在asp.net中的用法及作用
原文链接:http://www.cnblogs.com/Mr_JinRui/archive/2010/07/06/1772129.html Eval( " ")和Bind( &qu ...
- ASP.NET MVC5----基本用法
由于之前在项目中也使用MVC进行开发,但是具体是那个版本就不是很清楚了,但是我觉得大体的思想是相同的,只是版本高的在版本低的基础上增加了一些更加方便操作的东西.下面是我学习ASP.NET MVC5高级 ...
- C# ASP.NET MVC HtmlHelper用法大全
UrlHrlper 下面的两个地址一样的功能 下边这个防止路由规则改变 比如UserInfo/Index改为UserInfo-Index,使用下面的不受影响 另一种形式的超链接: <%: Htm ...
- asp.net Gridview 的用法
留个档. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="Fa ...
随机推荐
- 多点触摸(MT)协议(翻译)
参考: http://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt 转自:http://www.arm9home.ne ...
- Redis学习笔记2-Redis的安装体验
Redis的官方只提供了Linux版本的,并没提供Windows版本的(不过非官方有windows版本的.可以下载下来做开发测试学习用非常方便.博客后面会介绍到的).Linux下安装过程如下[以下命令 ...
- getSupportFragmentManager要用在FragmentActivity及其子类中
getSupportFragmentManager要用在FragmentActivity及其子类中!! 关于安卓抽屉导航!! * 自定义侧边栏
- WIN32下使用DirectSound接口的简单音频播放器(支持wav和mp3)
刚好最近接触了一些DirectSound,就写了一个小程序练练手,可以用来添加播放基本的wav和mp3音频文件的播放器.界面只是简单的GDI,dxsdk只使用了DirectSound8相关的接口. D ...
- delphi WebBrowser控件上网页验证码图片识别教程(一)
步骤一:获取网页中验证码图片的url地址 在delphi中加入一个BitBtn和一个memo以及WebBrowser控件实现网页中验证码图片的url地址的获取 程序如下:procedure TForm ...
- 【WPF系列】基础 PasswordBox
参考 How to bind to a PasswordBox in MVVM
- 在 KVM 上安装 Win7 虚拟机
之前都是在用Linux 虚机,现在有需要用到Win7 虚机,才发现在 KVM 上安装 Win7 的过程远比想象中的复杂.本文就把其过程做个简单总结. 1. 在 Virtual Machine Mana ...
- Bootstrap 按钮
本章将通过实例讲解如何使用 Bootstrap 按钮.任何带有 class .btn 的元素都会继承圆角灰色按钮的默认外观.但是 Bootstrap 提供了一些选项来定义按钮的样式,具体如下表所示: ...
- BZOJ1497: [NOI2006]最大获利[最小割 最大闭合子图]
1497: [NOI2006]最大获利 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 4375 Solved: 2142[Submit][Status] ...
- HDU3466 Proud Merchants[背包DP 条件限制]
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...