.net关于httpModules的应用示例
这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时 候又不太确定。本文通过一个简单的例子来直观的比较一下这三个对象的使用。 HttpModule:Http模块,可以在页面处理前后、应用程序初始化、出错等时候加入自己的事件处理程序 HttpHandler:Http处理程序,处理页面请求 HttpHandlerFactory:用来创建Http处理程序,创建的同时可以附加自己的事件处理程序
例子很简单,就是在每个页面的头部加入一个版权声明。 一、HttpModule 这个对象我们经常用来进行统一的权限判断、日志等处理。例子代码:
- public class MyModule : IHttpModule
- {
- public void Init(HttpApplication application)
- {
- application.BeginRequest += new EventHandler(application_BeginRequest);
- }
- void application_BeginRequest(object sender, EventArgs e)
- {
- ((HttpApplication)sender).Response.Write("Copyright @Gspring<br/>");
- }
- public void Dispose()
- {
- }
- }
- public class MyModule : IHttpModule
- {
- public void Init(HttpApplication application)
- {
- application.BeginRequest += new EventHandler(application_BeginRequest);
- }
- void application_BeginRequest(object sender, EventArgs e)
- {
- ((HttpApplication)sender).Response.Write("Copyright @Gspring<br/>");
- }
- public void Dispose()
- {
- }
- }
web.config中配置:
三、HttpHandlerFactory 这个对象也可以用来加入特殊的后缀所对应的处理程序,它的功能比HttpHandler要更加强大,在系统的web.config中就是通过注册HttpHandlerFactory来实现aspx页面的访问的:
- <add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>
- <add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>
HttpHandlerFactory是HttpHandler的工厂,通过它来生成不同的HttpHandler对象。 例子代码:
- public class MyHandlerFactory : 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)
- {
- }
- }
- public class MyHandlerFactory : 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)
- {
- }
- }
web.config中配置:
- <httpHandlers>
- <add verb="*" path="*.aspx" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
- </httpHandlers>
- <httpHandlers>
- <add verb="*" path="*.aspx" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
- </httpHandlers>
在例子中我们通过调用系统默认的PageHandlerFactory类进行常规处理,然后在处理过程中加入自己的代码,可以在Page对象上附加自己的事件处理程序。 附一个小的恶作剧: 我们可以开发好aspx页面,然后把web应用程序发布后把所有的aspx文件的后缀都改为spring,再在web.config中加入配置:
- <httpHandlers>
- <add verb="*" path="*.spring" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
- </httpHandlers>
- <compilation>
- <buildProviders>
- <add extension=".spring" type="System.Web.Compilation.PageBuildProvider"/>
- </buildProviders>
- </compilation>
- <httpHandlers>
- <add verb="*" path="*.spring" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
- </httpHandlers>
- <compilation>
- <buildProviders>
- <add extension=".spring" type="System.Web.Compilation.PageBuildProvider"/>
- </buildProviders>
- </compilation>
buildProviders是用来指定spring后缀的编译程序,我们把它设置成和aspx一致就可以了。如果在IIS中发布的话还需要在应用程序配置中加入spring的后缀映射。然后我们就可以通过 http://../.../*.spring来访问我们的网站了
- <add verb="*" path="*.aspx" type="HttpHandle.MyHandler, HttpHandle"/>
- </httpHandlers>
- <httpHandlers>
- <add verb="*" path="*.aspx" type="HttpHandle.MyHandler, HttpHandle"/>
- </httpHandlers>
这个对象主要就是ProcessRequest方法,在这个方法中输出版权信息,但同时也有一个问题:原来的页面不会被处理,也就是说页面中只有版权声明了。那么所有的aspx页面都不能正常运行了
.net关于httpModules的应用示例的更多相关文章
- HttpModule & HttpHandler
ASP.NET 处理请求的过程 inetinfo.exe:www 服务进程,IIS 服务 和 ASPNET_ISAPI.dll 都寄存在此进程中. ASPNET_ISAPI.dll:处理 .aspx ...
- 一步一步带你做WebApi迁移ASP.NET Core2.0
随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的ASP.NET WebApi站点,就可以跨平台运行在Linux中.我们有必要先说一下ASP.NET Core. ASP ...
- WebApi迁移ASP.NET Core2.0
WebApi迁移ASP.NET Core2.0 一步一步带你做WebApi迁移ASP.NET Core2.0 随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的AS ...
- .Net Core技术研究-WebApi迁移ASP.NET Core2.0
随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的ASP.NET WebApi站点,就可以跨平台运行在Linux中.我们有必要先说一下ASP.NET Core. ASP ...
- 采用HttpModules来重写URLS
首先写一个处理URLs重写的类,并且这个类必须继承IHttpHandler接口,以博客园的程序为例: public class UrlReWriteModule : System.Web.IHttpM ...
- 关于asp.net mvc中的httpModules 与 httpHandler
ASP.NET对请求处理的过程: 当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给ASPNET_ISAPI.dll,A ...
- httpModules 与 httpHandlers
ASP.NET对请求处理的过程:当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给ASPNET_ISAPI.dll,AS ...
- 浅谈Httpmodules
HttpModule是ASP.NET过滤器,可以理解为HTTP请求的必经之地我们只要实现IHttpModule接口,就可以取代HttpModule namespace BookShop.Handler ...
- ASP.NET中httpmodules与httphandlers全解析
https://www.cnblogs.com/zpc870921/archive/2012/03/12/2391424.html https://www.cnblogs.com/PiaoMiaoGo ...
随机推荐
- SimPholders2 模拟器 App 文件路径查看工具
SimPholder2.app 官网下载地址:http://www.simpholders.com 当使用 Xcode beta 版本切换到 Xcode 正式版本时,点击 SimPholders2. ...
- Makefile---make内嵌函数及make命令显示 (九)
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 这一节我们讲一下make的函数,在之前的章节已经讲到了几个函数:wildcard.patsubs ...
- knockoutjs foreach array绑定 表格 下拉框绑定
转载http://desert3.iteye.com/blog/1480471 knockoutjs foreach array绑定 表格 下拉框绑定 博客分类: Javascript 动态表格使 ...
- spring源码学习之:spring容器的applicationContext启动过程
Spring 容器像一台构造精妙的机器,我们通过配置文件向机器传达控制信息,机器就能够按照设定的模式进行工作.如果我们将Spring容器比喻为一辆汽车,可以将 BeanFactory看成汽车的发动机, ...
- tomcat 源码解析
how_tomcat_works https://www.uzh.ch/cmsssl/dam/jcr:00000000-29c9-42ee-0000-000074fab75a/how_tomcat_w ...
- Linux-IP地址后边加个/8(16,24,32)是什么意思?
是掩码的位数 A类IP地址的默认子网掩码为255.0.0.0(由于255相当于二进制的8位1,所以也缩写成“/8”,表示网络号占了8位); B类的为255.255.0.0(/16) ...
- 联表更新 Update Left Join
Update a set a.Manage_FunctID=b.Manage_FunctID From Manage_PageUrl a Left join Manage_ButtonBar b on ...
- Java 异常处理机制和集合框架
一.实验目的 掌握面向对象程序设计技术 二.实验环境 1.微型计算机一台 2.WINDOWS操作系统,Java SDK,Eclipse开发环境 三.实验内容 1.Java异常处理机制涉及5个关键字:t ...
- for 与 foreach 性能
For 与Foreach 性能 差别在不同的场景下会有不同的差异. 对于不同的目标 , 如 T[] 与 IEnumerable<T> 两个的性能就感觉出来了,对于T[] 都快. ...
- 如何面试程序员 zhuan zai
zhuan zai http://blog.csdn.net/cuibo1123/article/details/41931909aia 面试对于大多数开发人员来说是一项很基本的技能.一次失败的招聘 ...