描述三个C#对象的使用

开发 后端
本文介绍三个C#对象的使用我们在开发ASP.NET程序时经常会用到,通过一个简单的例子来直观的比较一下这三个C#对象的使用。

本文介绍HttpModule,HttpHandler,HttpHandlerFactory简单使用,这三个C#对象的使用我们在开发ASP.NET程序时经常会用到,似乎很熟悉,但有时候又不太确定。本文通过一个简单的例子来直观的比较一下这三个C#对象的使用。
◆HttpModule:Http模块,可以在页面处理前后、应用程序初始化、出错等时候加入自己的事件处理程序
◆HttpHandler:Http处理程序,处理页面请求
◆HttpHandlerFactory:用来创建Http处理程序,创建的同时可以附加自己的事件处理程序
例子很简单,就是在每个页面的头部加入一个版权声明。

一、HttpModule

这个对象我们经常用来进行统一的权限判断、日志等处理。
例子代码:

  1. publicclassMyModule:IHttpModule  
  2. {  
  3. publicvoidInit(HttpApplicationapplication)  
  4. {  
  5. application.BeginRequest+=newEventHandler(application_BeginRequest);  
  6. }  
  7.  
  8. voidapplication_BeginRequest(objectsender,EventArgse)  
  9. {  
  10. ((HttpApplication)sender).Response.Write("Copyright@Gspring<br/>");  
  11. }  
  12.  
  13. publicvoidDispose()  
  14. {  
  15. }  

在Init方法中可以注册很多application的事件,我们的例子就是在开始请求的时候加入自己的代码,将版权声明加到页面的头部

二、HttpHandler

这个对象经常用来加入特殊的后缀所对应的处理程序,比如可以限制.doc的文件只能给某个权限的人访问。
Asp.Net中的Page类就是一个IHttpHandler的实现
例子代码:

  1. publicclassMyHandler:IHttpHandler  
  2. {  
  3. publicvoidProcessRequest(HttpContextctx)  
  4. {  
  5. ctx.Response.Write("Copyright@Gspring<br/>");  
  6. }  
  7. publicboolIsReusable  
  8. {  
  9. get{returntrue;}  
  10. }  
  11. }  

这个对象主要就是ProcessRequest方法,在这个方法中输出版权信息,但同时也有一个问题:原来的页面不会被处理,也就是说页面中只有版权声明了。那么所有的aspx页面都不能正常运行了

三、HttpHandlerFactory

这个对象也可以用来加入特殊的后缀所对应的处理程序,它的功能比HttpHandler要更加强大,在系统的web.config中就是通过注册HttpHandlerFactory来实现aspx页面的访问的。

HttpHandlerFactory是HttpHandler的工厂,通过它来生成不同的HttpHandler对象。

  1. publicclassMyHandlerFactory:IHttpHandlerFactory  
  2. {  
  3. publicIHttpHandlerGetHandler(HttpContextcontext,stringrequestType,
    stringurl,stringpathTranslated)  
  4. {  
  5. PageHandlerFactoryfactory=(PageHandlerFactory)Activator.
    CreateInstance(typeof(PageHandlerFactory),true);  
  6. IHttpHandlerhandler=factory.GetHandler
    (context,requestType,url,pathTranslated);  
  7.  
  8. //执行一些其它操作  
  9. Execute(handler);  
  10.  
  11. returnhandler;  
  12. }  
  13.  
  14. privatevoidExecute(IHttpHandlerhandler)  
  15. {  
  16. if(handlerisPage)  
  17. {  
  18. //可以直接对Page对象进行操作  
  19. ((Page)handler).PreLoad+=newEventHandler(MyHandlerFactory_PreLoad);  
  20. }  
  21. }  
  22.  
  23. voidMyHandlerFactory_PreLoad(objectsender,EventArgse)  
  24. {  
  25. ((Page)sender).Response.Write("Copyright@Gspring<br/>");  
  26. }  
  27.  
  28. publicvoidReleaseHandler(IHttpHandlerhandler)  
  29. {  
  30. }  

以上介绍三个C#对象的使用。

【编辑推荐】

  1. 分析C#不安全代码
  2. 浅析C#调用ImageAnimator
  3. C#连接Access、SQL Server数据库
  4. 浅谈C#固定的和活动的变量
  5. 介绍C#中的值类型
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-03 13:43:02

C#日历控件

2009-08-21 17:48:13

C#读取文件信息

2009-08-27 10:31:39

C#对象初始化器

2009-08-07 15:49:46

使用C#字符串

2009-07-03 17:01:30

JSP2JSP

2009-08-13 17:04:09

C#语言C#程序

2009-08-13 17:22:15

C#数据集

2009-08-20 09:30:03

C#开发WinForm

2009-08-03 16:45:02

C#异步Socket

2009-08-17 16:32:34

C# Anonymou

2009-08-31 13:18:09

C# IWebMess

2009-08-31 18:32:01

C# ListBoxE

2009-08-03 18:08:39

C# ICloneab

2009-08-26 17:49:36

C# readonly

2009-08-18 17:41:22

C# ListView

2009-09-07 15:49:55

C#属性化的方法

2009-08-19 10:09:21

C#和C++

2023-03-28 23:27:36

C#编程

2009-09-07 15:31:49

C#支持事件

2009-09-07 13:02:52

Java和C#线程
点赞
收藏

51CTO技术栈公众号