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 ...
随机推荐
- 嵌入式C语言不可不用的关键字
1.static关键字 这个关键字前面也有提到,它的作用是强大的. 要对static关键字深入了解,首先需要掌握标准C程序的组成. 标准C程序一直由下列部分组成: 1)正文段——CPU执行的机器指令部 ...
- 【Xamarin挖墙脚系列:最重要的布局ListView】
原文:[Xamarin挖墙脚系列:最重要的布局ListView] 安卓的几个重要的布局 线性布局 相对布局 Table布局 Tab布局 表格Grid布局 列表布局. 这几种基本的布局的方式,最重要 ...
- zabbix 通过key 获取
zabbix:/root# zabbix_get -s 192.168.2.224 -k "perf_counter[\Processor(_Total)\% Processor Time] ...
- Linux kernel 内存损坏漏洞
漏洞名称: Linux kernel 内存损坏漏洞 CNNVD编号: CNNVD-201310-143 发布时间: 2013-10-11 更新时间: 2013-10-11 危害等级: 中危 漏洞类 ...
- 实验记录贴 —— 账号同步实验 RTX 和 LDAP(AD域)
目前,公司有多个系统,RTX,邮箱(MD),OA,NC. 这些系统之间,如果要实现单点登录的话,账户肯定需要同步,或者某一种映射机制. 如果所有数据都和中央账号数据库(LDAP,这里是AD域)看齐,那 ...
- android Spinner的使用
首先是MainActivity package com.example.spinnertest; import java.util.ArrayList; import java.util.List; ...
- 【转】win7 虚拟机virtualbox中ubuntu12.04安装samba实现文件共享
原文网址:http://blog.csdn.net/watkinsong/article/details/8878786 昨天心血来潮,又装了个虚拟机,然后安装了ubuntu12.04,为了实现在虚拟 ...
- FileUpload上传文件无法获取文件名
原因:将FileUpload控件放到了UpdatePannel控件中了 解决办法:将FileUpload控件位置移动到UpdatePannel控件外面
- HTML5与CSS3权威指南.pdf7
第14章 使用选择器在页面中插入内容 使用选择器来插入文字图片 上一章提到过使用before和after伪类为某个元素之前或之后插入内容 h2:after{content:'12345'},也可以指定 ...
- linux 双网关双IP设置
server:CentOS5.8 ip:172.16.8.11 Gateway:172.16.8.1 ip:10.120.6.78 Gateway:10.120.6.1 网卡配置: eth0 poin ...