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 ...
随机推荐
- EasyUI datagrid数据表格的函数getData返回来的是什么
EasyUI datagrid数据表格的函数getData返回来的是什么? 他返回来的是这么一个对象: Object { rows=[10], total=15} 其中rows就是每一行的数据,是这些 ...
- 设计模式之单例(singleton)设计模式代码详解
单例有两种:懒汉式和饿汉式 /** * 懒汉式的单例模式 * 这种单例模式如果采用到多线程调用该方法,有可能会产生多个实例,原因是: * 当线程一进入了①处,此时轮到线程二的时间片,线程二也来到①处, ...
- Android Training精要(二)開啟ActionBar的Overlay模式
在3.0上的實現 <resources> <!-- the theme applied to the application or activity --> <style ...
- SFTP文件上传与下载
SFTP是对ftp进行加密的一种文件协议. 首先是先引入:jsch-0.1.42-sources.jar与jsch-0.1.42.jar包 SFTP文件上传与下载. import java.io.Fi ...
- 【HDOJ】3315 My Brute
几乎与2853相同,MCMF. /* 2853 */ #include <iostream> #include <string> #include <map> #i ...
- oracle命中率模型计算
命中率模型是在owi之前比较常用的一种诊断性能问题的方法,通过命中率的计算,发现系统中的一些设置是否合理,当命中率不高的时候,通过调整一些参数和设置,提高命中率,有效的提高系统的性能和吞吐量.但当系统 ...
- Node v4.1.1
Installing Node.js via package manager curl --silent --location https://deb.nodesource.com/setup_4.x ...
- [PeterDLax著泛函分析习题参考解答]第7章 Hilbert 空间结果的应用
1. 对测度是 $\sigma$ 有限的情形证明 Radon-Nikodym 定理. 证明: 设 $\mu,\nu$ 均为 $\sigma$ 有限的非负测度, 则存在分割 $$\bex X=\cup_ ...
- devi into python 笔记(一)字典 列表的简单操作
模块: python中模块是很重要的概念,在模块中加入 if __name__ == "__main__": pass 会使模块应用起来更加灵活,避免出错. =========== ...
- SCI写作经典替换词,瞬间高大上!(转)
转自微信:http://mp.weixin.qq.com/s?__biz=MzA4NDk3ODEwNQ==&mid=202006470&idx=2&sn=e8900b94448 ...