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 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
 | 
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;namespace HttpModuleCK{    public class UserControl : IHttpModule    {        public void Dispose()        { }        public void Init(HttpApplication context)        {            context.BeginRequest += new System.EventHandler(httpApplication_BeginRequest);        }        public void httpApplication_BeginRequest(object sender, EventArgs e)        {            HttpApplication httpApplication = (HttpApplication)sender;            string url = httpApplication.Context.Request.Path;            //做用户权限控制            HttpContext content = (HttpContext)httpApplication.Context;            //if (httpApplication.Request["userid"] == null)            //{            //    content.Response.Write("未提供必需的参数!!");            //    content.Response.End();            //}        }    }} | 
在Web项目中添加引用该类,添加配置信息即可,
| 
 1 
2 
3 
 | 
<httpModules>      <add name="UserRight" type="HttpModuleCK.UserControl, HttpModuleCK"/>           </httpModules> | 
2.HttpHandler
这个常用来做文件访问控制,图片防盗链等等.
新建类库添加如下代码:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
 | 
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;namespace HttpHandlerCK{    public class docHandle:IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            string FileName = context.Server.MapPath(context.Request.FilePath);            if (context.Request["userid"] == null)            {                context.Response.Write("doc文件无法访问!!");                context.Response.End();            }                    }        public bool IsReusable        {            get            {                return false;            }        }    }}在Web项目的config中配置如下: | 
| 
 1 
2 
3 
 | 
<httpHandlers>  <add verb="*" path="*.doc" type="HttpHandlerCK.docHandle, HttpHandlerCK"/>     </httpHandlers> | 
3.HttpHandlerFactory
功能比HttpHandler要更加强大,HttpHandlerFactory是HttpHandler的工厂,通过它来生成不同的HttpHandler对象。
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
 | 
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;namespace HandlerFactoryCK{    public class aspxHandle: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)        {        }    }}  在Webconfig配置如下: | 
| 
 1 
2 
3 
 | 
    <httpHandlers>      <add verb="*" path="*.aspx" type="HandlerFactoryCK.aspxHandle, HandlerFactoryCK"/></httpHandlers> | 
三者的区别如下:
生命周期中涉及到几个非常重要的对象:HttpHandler,HttpModule,IHttpHandlerFactory,他们的执行(顺序)大致的执行过程是这样的:
client端发送页面请求,被IIS的某个进程截获,它根据申请的页面后缀(.aspx)不同,调用不同的页面处理程序(.asp->asp.dll; .aspx->ISAPI.dll)。而页面处理程序在处理过程中,则要经历HttpModule,HttpHandler的处理:前者HttpModule用于页面处理前和处理后的一些事件的处理,后者HttpHandler进行真正的页面的处理。 
如前所说,HttpModule会在页面处理前和后对页面进行处理,所以它不会影响真正的页面请求。通常用在给每个页面的头部或者尾部添加一些信息(如版权声明)等。曾经见过一些免费的空间,我们的页面上传上去后,浏览的时候发现,在每个页面的头部和尾部多了很多小广告....如果理解了HttpModule的原理,要做这个就不是很难了~
IHttpModule与IHttpHandler的区别整理 
1.先后次序.先IHttpModule,后IHttpHandler. 注:Module要看你响应了哪个事件,一些事件是在Handler之前运行的,一些是在Handler之后运行的 
2.对请求的处理上: 
   IHttpModule是属于大小通吃类型,无论客户端请求的是什么文件,都会调用到它;例如aspx,rar,html的请求. 
   IHttpHandler则属于挑食类型,只有ASP.net注册过的文件类型(例如aspx,asmx等等)才会轮到调用它. 
3.IHttpHandler按照你的请求生成响应的内容,IHttpModule对请求进行预处理,如验证、修改、过滤等等,同时也可以对响应进行处理
HttpModule HttpHandler HttpHandlerFactory 学习笔记的更多相关文章
- 【Ext.Net学习笔记】01:在ASP.NET WebForm中使用Ext.Net
		
Ext.NET是基于跨浏览器的ExtJS库和.NET Framework的一套支持ASP.NET AJAX的开源Web控件,包含有丰富的Ajax运用,其前身是Coolite. 下载地址:http:// ...
 - Ext.Net学习笔记01:在ASP.NET WebForm中使用Ext.Net
		
Ext.Net是一个对ExtJS进行封装了的.net控件库,可以在ASP.NET WebForm和MVC中使用.从今天开始记录我的学习笔记,这是第一篇,今天学习了如何在WebForm中使用Ext.Ne ...
 - Asp.Net Core WebApi学习笔记(四)-- Middleware
		
Asp.Net Core WebApi学习笔记(四)-- Middleware 本文记录了Asp.Net管道模型和Asp.Net Core的Middleware模型的对比,并在上一篇的基础上增加Mid ...
 - <ASP.NET4 从入门到精通>学习笔记1
		
非常久没有写东西了,今日有时间,開始整理一下关于ASP.NET 4的学习笔记.提醒自己,也欢迎评论. 概述一共分为6个大的部分,兴许文章.将依据每一个部分进行整理,本读书笔记仅仅是整理关键点,对于啰嗦 ...
 - ASP.Net开发基础温故知新学习笔记
		
申明:本文是学习2014版ASP.Net视频教程的学习笔记,仅供本人复习之用,也没有发布到博客园首页. 一.一般处理程序基础 (1)表单提交注意点: ①GET通过URL,POST通过报文体: ②需在H ...
 - ASP.NET 管道事件与HttpModule, HttpHandler简单理解 -摘自网络
		
第一部分:转载自Artech IIS与ASP.NET管道 ASP.NET管道 以IIS 6.0为例,在工作进程w3wp.exe中,利用Aspnet_ispai.dll加载.NET运行时(如果.NET ...
 - python网络爬虫学习笔记
		
python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...
 - ASP.NET三剑客 HttpApplication HttpModule  HttpHandler 解析
		
我们都知道,ASP.Net运行时环境中处理请求是通过一系列对象来完成的,包含HttpApplication,HttpModule, HttpHandler.之所以将这三个对象称之为ASP.NET三剑客 ...
 - Asp.Net 学习笔记(IIS不同版本和Asp.Net)
		
主要目的是在网上记录一下学习笔记,如有不对,请指出 谢谢!! iis5.x: 存在问题,inet info收到动态请求后,aspnt_isapi.dll会被加载到inetinfo.exe(挂载w3sv ...
 
随机推荐
- 带圆角的EditText
			
转载请注明出处:http://blog.csdn.net/krislight/article 1.定义一个Drawable <?xml version="1.0" encod ...
 - Android Training精要(四) Intent注意事项
			
判断有处理Intent的Activity PackageManager packageManager = getPackageManager(); List<ResolveInfo> ac ...
 - redis key expire
			
EXPIRE key seconds 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除. 在 Redis 中,带有生存时间的 key 被称为『易失的』(volati ...
 - ServiceModel Metadata Utility Tool (Svcutil.exe)
			
https://msdn.microsoft.com/zh-cn/library/aa347733.aspx 参数: /directory:<directory> Directory to ...
 - TCP三次握手四次断开
			
今天被问到三次握手了,当时只是脑子里有印象,却忘了一些SYN细节,手动微笑. 这么下去还怎么混...赶紧复习个... 三次握手是什么? TCP是面向连接的,无论哪一方向另一方发送数据之前,都必须先在双 ...
 - WPA Supplicant 用法
			
本文译至:https://wiki.archlinux.org/index.php/WPA_Supplicant_%28%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%29 ...
 - Excel和XML文件导入
			
using System;using System.Collections;using System.Collections.Generic;using System.Configuration;us ...
 - Hibernate不同数据库 方言|驱动|url 配置
			
Hibernate不同数据库方言|驱动|url mySql: hibernate.dialect : org.hibernate.dialect.MySQLDialect driverClassNam ...
 - sqrt (x) 牛顿迭代法
			
参考: 0开方 是 0 1的开方式 1 2的开方式 1.4 3.的开方=(1.4+3/1.4)/2 牛顿迭代法:学习自 http://blog.csdn.net/youwuwei2012/articl ...
 - jQuery技术内幕预览版.pdf2
			
第二章 构造jQuery对象 jQuery对象是一个类数组对象,含有连续的整型属性.length属性和大量的jQuery方法,$()是jQuery()的缩写 构造函数jQuery() 如果调用构造函数 ...