C#强化系列:HttpModule,HttpHandler,HttpHandlerFactory简单使用
这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定。本文通过一个简单的例子来直观的比较一下这三个对象的使用。
HttpModule:Http模块,可以在页面处理前后、应用程序初始化、出错等时候加入自己的事件处理程序
HttpHandler:Http处理程序,处理页面请求
HttpHandlerFactory:用来创建Http处理程序,创建的同时可以附加自己的事件处理程序
例子很简单,就是在每个页面的头部加入一个版权声明。
一、HttpModule
这个对象我们经常用来进行统一的权限判断、日志等处理。
例子代码:
public class MyModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(application_BeginRequest);
}
void application_BeginRequest(object sender, EventArgs e)
{
((HttpApplication)sender).Response.Write("Copyright @Gspring<br/>");
}
public void Dispose()
{
}
}
web.config中配置:
<httpModules>
<add name="test" type="HttpHandle.MyModule, HttpHandle"/>
</httpModules>在Init方法中可以注册很多application的事件,我们的例子就是在开始请求的时候加入自己的代码,将版权声明加到页面的头部
二、HttpHandler
这个对象经常用来加入特殊的后缀所对应的处理程序,比如可以限制.doc的文件只能给某个权限的人访问。
Asp.Net中的Page类就是一个IHttpHandler的实现
例子代码:
public class MyHandler : IHttpHandler
{
public void ProcessRequest(HttpContext ctx)
{
ctx.Response.Write("Copyright @Gspring<br/>");
}
public bool IsReusable
{
get { return true; }
}
}
web.config中配置:
<httpHandlers>
<add verb="*" path="*.aspx" type="HttpHandle.MyHandler, HttpHandle"/>
</httpHandlers>
这个对象主要就是ProcessRequest方法,在这个方法中输出版权信息,但同时也有一个问题:原来的页面不会被处理,也就是说页面中只有版权声明了。那么所有的aspx页面都不能正常运行了
三、HttpHandlerFactory
这个对象也可以用来加入特殊的后缀所对应的处理程序,它的功能比HttpHandler要更加强大,在系统的web.config中就是通过注册HttpHandlerFactory来实现aspx页面的访问的:
<add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>
HttpHandlerFactory是HttpHandler的工厂,通过它来生成不同的HttpHandler对象。
例子代码:
public class MyHandlerFactory : IHttpHandlerFactory
{
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
PageHandlerFactory factory = (PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory), true);
IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated);
//执行一些其它操作
Execute(handler);
return handler;
}
private void Execute(IHttpHandler handler)
{
if (handler is Page)
{
//可以直接对Page对象进行操作
((Page)handler).PreLoad += new EventHandler(MyHandlerFactory_PreLoad);
}
}
void MyHandlerFactory_PreLoad(object sender, EventArgs e)
{
((Page)sender).Response.Write("Copyright @Gspring<br/>");
}
public void ReleaseHandler(IHttpHandler handler)
{
}
}web.config中配置:
<httpHandlers>
<add verb="*" path="*.aspx" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
</httpHandlers>在例子中我们通过调用系统默认的PageHandlerFactory类进行常规处理,然后在处理过程中加入自己的代码,可以在Page对象上附加自己的事件处理程序。
附一个小的恶作剧:
我们可以开发好aspx页面,然后把web应用程序发布后把所有的aspx文件的后缀都改为spring,再在web.config中加入配置:
<httpHandlers>
<add verb="*" path="*.spring" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
</httpHandlers>
<compilation>
<buildProviders>
<add extension=".spring" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
</compilation>buildProviders是用来指定spring后缀的编译程序,我们把它设置成和aspx一致就可以了。如果在IIS中发布的话还需要在应用程序配置中加入spring的后缀映射。
然后我们就可以通过 http://../.../*.spring来访问我们的网站了![]()
出处:https://www.cnblogs.com/firstyi/archive/2008/05/07/1187274.html
C#强化系列:HttpModule,HttpHandler,HttpHandlerFactory简单使用的更多相关文章
- HttpModule HttpHandler HttpHandlerFactory 学习笔记
1.HttpModule 最常见的是使用HttpModule来做页面权限控制. 在新建类库添加如下代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- ASP.NET 管道事件与HttpModule, HttpHandler简单理解 -摘自网络
第一部分:转载自Artech IIS与ASP.NET管道 ASP.NET管道 以IIS 6.0为例,在工作进程w3wp.exe中,利用Aspnet_ispai.dll加载.NET运行时(如果.NET ...
- HttpModule & HttpHandler
ASP.NET 处理请求的过程 inetinfo.exe:www 服务进程,IIS 服务 和 ASPNET_ISAPI.dll 都寄存在此进程中. ASPNET_ISAPI.dll:处理 .aspx ...
- ASP.NET三剑客 HttpApplication HttpModule HttpHandler 解析
我们都知道,ASP.Net运行时环境中处理请求是通过一系列对象来完成的,包含HttpApplication,HttpModule, HttpHandler.之所以将这三个对象称之为ASP.NET三剑客 ...
- 《zw版·Halcon-delphi系列原创教程》简单的令人发指,只有10行代码的车牌识别脚本
<zw版·Halcon-delphi系列原创教程>简单的令人发指,只有10行代码的车牌识别脚本 简单的令人发指,只有10行代码的车牌识别脚本 人脸识别.车牌识别是opencv当中 ...
- 用Qt写软件系列三:一个简单的系统工具(上)
导言 继上篇<用Qt写软件系列二:QIECookieViewer>之后,有一段时间没有更新博客了.这次要写的是一个简单的系统工具,需求来自一个内部项目.功能其实很简单,就是查看当前当前系统 ...
- [Ruby on Rails系列]6、一个简单的暗语生成器与解释器(上)
[0]Ruby on Rails 系列回顾 [Ruby on Rails系列]1.开发环境准备:Vmware和Linux的安装 [Ruby on Rails系列]2.开发环境准备:Ruby on Ra ...
- ASP.NET (HttpModule,HttpHandler)
asp.net 事件模型机制 ----------------------- 一 客户的请求页面由aspnet_isapi.dll这个动态连接库来处理,把请求的aspx文件发送给CLR进行编译执行,然 ...
- scrapy爬虫学习系列二:scrapy简单爬虫样例学习
系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备: http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...
随机推荐
- [Angular] Performance Caching Policy - Cache First, Network Last
If you want to cache API response by using angular service-worker, you can do it in: src/ngsw-config ...
- BNU 34986 Football on Table
"Bored? Let's play table football!" The table football is played on a rectangular table, u ...
- LLVM每日谈之十九 LLVM的第一本系统的书<Getting Started with LLVM Core Libraries>
作者:史宁宁(snsn1984) LLVM最终有了一本系统的书了--<Getting Started with LLVM Core Libraries>. 这本书号称是LLVM的第一本书, ...
- Centos安装FastDFS+Nginx
一.安装环境: gcc:安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc: yum install gcc-c++ PCRE:PCRE(Perl C ...
- Triangle LOVE(拓扑排序)
Triangle LOVE Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) Total ...
- html5之文件操作
用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API,使用该API可以在浏览器主线程中异步访问文件系统,读取文件中的数据.到目前文职,只有FF3.6+和Chrome6 ...
- Hdu-6119 小小粉丝度度熊 尺取
题面 题意:在一大段时间里,告诉你,你签到了哪些区间,现在再给你m张补签卡,问你最多能实现连续签到多少天 题解:那些时间区间是有重叠的,所以我们先排序离散,并得到哪些区间是可以补签的,这样问题就变成, ...
- c语言递归讲解分析
C语言允许函数调用它自己,这种调用的过程称为"递归(recursion)" 举例说明,如下代码: #include <stdio.h> void up_and_down ...
- mysql 操作提示 1366 Incorrect string value
一.报错说明 数据库此字段的字符集与整理字符集是否与SQL语句传递数据的字符集相同:不相同则会引发MySQL1366错误. 二.产生原因 windows 安装MySql 的时候选择的是默认的编码,创建 ...
- HTML网页做成ASP.NET后台的方法以及.NET后台控制前台样式的方法
之前一直不知道,写好的纯HTML网页怎么做成ASP.NET后台的呢,因为之前使用别人的HTML模板写过一个自己的个人博客 果冻栋吖个人博客 当时用的PHP写的.一直在考虑怎么做成.NET的. 今天自己 ...