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

例子很简单,就是在每个页面的头部加入一个版权声明。 一、HttpModule 这个对象我们经常用来进行统一的权限判断、日志等处理。例子代码:

  1. public class MyModule : IHttpModule
  2. {
  3. public void Init(HttpApplication application)
  4. {
  5. application.BeginRequest += new EventHandler(application_BeginRequest);
  6. }
  7. void application_BeginRequest(object sender, EventArgs e)
  8. {
  9. ((HttpApplication)sender).Response.Write("Copyright @Gspring<br/>");
  10. }
  11. public void Dispose()
  12. {
  13. }
  14. }
  1. public class MyModule : IHttpModule
  2. {
  3. public void Init(HttpApplication application)
  4. {
  5. application.BeginRequest += new EventHandler(application_BeginRequest);
  6. }
  7. void application_BeginRequest(object sender, EventArgs e)
  8. {
  9. ((HttpApplication)sender).Response.Write("Copyright @Gspring<br/>");
  10. }
  11. public void Dispose()
  12. {
  13. }
  14. }

web.config中配置:

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

  1. <add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>
  1. <add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>

HttpHandlerFactory是HttpHandler的工厂,通过它来生成不同的HttpHandler对象。 例子代码:

  1. public class MyHandlerFactory : IHttpHandlerFactory
  2. {
  3. public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
  4. {
  5. PageHandlerFactory factory = (PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory), true);
  6. IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated);
  7. //执行一些其它操作
  8. Execute(handler);
  9. return handler;
  10. }
  11. private void Execute(IHttpHandler handler)
  12. {
  13. if (handler is Page)
  14. {
  15. //可以直接对Page对象进行操作
  16. ((Page)handler).PreLoad += new EventHandler(MyHandlerFactory_PreLoad);
  17. }
  18. }
  19. void MyHandlerFactory_PreLoad(object sender, EventArgs e)
  20. {
  21. ((Page)sender).Response.Write("Copyright @Gspring<br/>");
  22. }
  23. public void ReleaseHandler(IHttpHandler handler)
  24. {
  25. }
  26. }
  1. public class MyHandlerFactory : IHttpHandlerFactory
  2. {
  3. public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
  4. {
  5. PageHandlerFactory factory = (PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory), true);
  6. IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated);
  7. //执行一些其它操作
  8. Execute(handler);
  9. return handler;
  10. }
  11. private void Execute(IHttpHandler handler)
  12. {
  13. if (handler is Page)
  14. {
  15. //可以直接对Page对象进行操作
  16. ((Page)handler).PreLoad += new EventHandler(MyHandlerFactory_PreLoad);
  17. }
  18. }
  19. void MyHandlerFactory_PreLoad(object sender, EventArgs e)
  20. {
  21. ((Page)sender).Response.Write("Copyright @Gspring<br/>");
  22. }
  23. public void ReleaseHandler(IHttpHandler handler)
  24. {
  25. }
  26. }

web.config中配置:

  1. <httpHandlers>
  2. <add verb="*" path="*.aspx" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
  3. </httpHandlers>
  1. <httpHandlers>
  2. <add verb="*" path="*.aspx" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
  3. </httpHandlers>

在例子中我们通过调用系统默认的PageHandlerFactory类进行常规处理,然后在处理过程中加入自己的代码,可以在Page对象上附加自己的事件处理程序。 附一个小的恶作剧: 我们可以开发好aspx页面,然后把web应用程序发布后把所有的aspx文件的后缀都改为spring,再在web.config中加入配置:

  1. <httpHandlers>
  2. <add verb="*" path="*.spring" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
  3. </httpHandlers>
  4. <compilation>
  5. <buildProviders>
  6. <add extension=".spring" type="System.Web.Compilation.PageBuildProvider"/>
  7. </buildProviders>
  8. </compilation>
  1. <httpHandlers>
  2. <add verb="*" path="*.spring" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
  3. </httpHandlers>
  4. <compilation>
  5. <buildProviders>
  6. <add extension=".spring" type="System.Web.Compilation.PageBuildProvider"/>
  7. </buildProviders>
  8. </compilation>

buildProviders是用来指定spring后缀的编译程序,我们把它设置成和aspx一致就可以了。如果在IIS中发布的话还需要在应用程序配置中加入spring的后缀映射。然后我们就可以通过 http://../.../*.spring来访问我们的网站了

  1. <add verb="*" path="*.aspx" type="HttpHandle.MyHandler, HttpHandle"/>
  2. </httpHandlers>
  1. <httpHandlers>
  2. <add verb="*" path="*.aspx" type="HttpHandle.MyHandler, HttpHandle"/>
  3. </httpHandlers>

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

.net关于httpModules的应用示例的更多相关文章

  1. HttpModule & HttpHandler

    ASP.NET 处理请求的过程 inetinfo.exe:www 服务进程,IIS 服务 和 ASPNET_ISAPI.dll 都寄存在此进程中. ASPNET_ISAPI.dll:处理 .aspx ...

  2. 一步一步带你做WebApi迁移ASP.NET Core2.0

    随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的ASP.NET WebApi站点,就可以跨平台运行在Linux中.我们有必要先说一下ASP.NET Core. ASP ...

  3. WebApi迁移ASP.NET Core2.0

    WebApi迁移ASP.NET Core2.0 一步一步带你做WebApi迁移ASP.NET Core2.0   随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的AS ...

  4. .Net Core技术研究-WebApi迁移ASP.NET Core2.0

    随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的ASP.NET WebApi站点,就可以跨平台运行在Linux中.我们有必要先说一下ASP.NET Core. ASP ...

  5. 采用HttpModules来重写URLS

    首先写一个处理URLs重写的类,并且这个类必须继承IHttpHandler接口,以博客园的程序为例: public class UrlReWriteModule : System.Web.IHttpM ...

  6. 关于asp.net mvc中的httpModules 与 httpHandler

    ASP.NET对请求处理的过程: 当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给ASPNET_ISAPI.dll,A ...

  7. httpModules 与 httpHandlers

    ASP.NET对请求处理的过程:当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给ASPNET_ISAPI.dll,AS ...

  8. 浅谈Httpmodules

    HttpModule是ASP.NET过滤器,可以理解为HTTP请求的必经之地我们只要实现IHttpModule接口,就可以取代HttpModule namespace BookShop.Handler ...

  9. ASP.NET中httpmodules与httphandlers全解析

    https://www.cnblogs.com/zpc870921/archive/2012/03/12/2391424.html https://www.cnblogs.com/PiaoMiaoGo ...

随机推荐

  1. IOS 开发qq登陆界面

    // //  ViewController.m //  QQUI_bydfg // //  Created by Kevin_dfg on 16/4/15. //  Copyright © 2016年 ...

  2. C++@类的静态成员变量和静态成员函数

    参考: http://blog.csdn.net/morewindows/article/details/6721430 http://www.cnblogs.com/lzjsky/archive/2 ...

  3. phpstorm用正则删除PHP代码空行小技巧

    有很多小伙伴会遇到代码空行特别多,但是一行一行删除肯定很烦躁,这时候就需要用到批量删除空行. 怎么批量删除空行呢? 我的办法是用正则把所有空行找到,然后一键全部替换. 首先把Match Case和Re ...

  4. js类型判断

    console.log('---------------------'); var a="string"; console.log(a); //string var a=1; co ...

  5. Resource Manager

    Azure Resource Manager overview https://azure.microsoft.com/en-us/documentation/articles/resource-gr ...

  6. mysql学习之-show table status(获取表的信息)参数说明

    --获取表的信息mysql> show table status like 'columns_priv'\G;*************************** 1. row ******* ...

  7. C# 数组,ArrayList与List对象的区别

    在C#中,当我们想要存储一组对象的时候,就会想到用数组,ArrayList,List这三个对象了.那么这三者到底有什么样的区别呢? 我们先来了解一下数组,因为数组在C#中是最早出现的. 数组 数组有很 ...

  8. 【转】asp.net连接数据库字符串有哪些写法[数据连接]

    来源:http://blog.unvs.cn/archives/database-link-method.html 下面是个人总结的net网站连接数据库的字符串几种写法: 一.结合webconfig位 ...

  9. RSA非对称加密 php的openssl实现

    <?php /** * 使用openssl实现非对称加密 * @since 2010-07-08 */ class Rsa { /** * private key */ private $_pr ...

  10. struts2介绍

    struts2简介 Struts2框架发展 Struts于2000年5月由Craig McClanahan发起,并于 2001年7月发布了1.0版本,Struts一出现便大受欢迎,更成为了以后几年内w ...