zoukankan      html  css  js  c++  java
  • .net关于httpModules的应用示例

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

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

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

    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. {   
    4.   
    5.     public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)   
    6.   
    7.      {   
    8.   
    9.          PageHandlerFactory factory = (PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory), true);   
    10.   
    11.          IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated);   
    12.   
    13.   
    14.   
    15.         //执行一些其它操作   
    16.   
    17.          Execute(handler);   
    18.   
    19.   
    20.   
    21.         return handler;   
    22.   
    23.      }   
    24.   
    25.   
    26.   
    27.     private void Execute(IHttpHandler handler)   
    28.   
    29.      {   
    30.   
    31.         if (handler is Page)   
    32.   
    33.          {   
    34.   
    35.             //可以直接对Page对象进行操作   
    36.   
    37.              ((Page)handler).PreLoad += new EventHandler(MyHandlerFactory_PreLoad);   
    38.   
    39.          }   
    40.   
    41.      }   
    42.   
    43.   
    44.   
    45.     void MyHandlerFactory_PreLoad(object sender, EventArgs e)   
    46.   
    47.      {   
    48.   
    49.          ((Page)sender).Response.Write("Copyright @Gspring<br/>");   
    50.   
    51.      }   
    52.   
    53.   
    54.   
    55.     public void ReleaseHandler(IHttpHandler handler)   
    56.   
    57.      {   
    58.   
    59.      }   
    60.   
    61. }  
    1. public class MyHandlerFactory : IHttpHandlerFactory  
    2.   
    3.     {  
    4.   
    5.         public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)  
    6.   
    7.         {  
    8.   
    9.             PageHandlerFactory factory = (PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory), true);  
    10.   
    11.             IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated);  
    12.   
    13.             //执行一些其它操作  
    14.   
    15.             Execute(handler);  
    16.   
    17.             return handler;  
    18.   
    19.         }  
    20.   
    21.         private void Execute(IHttpHandler handler)  
    22.   
    23.         {  
    24.   
    25.             if (handler is Page)  
    26.   
    27.             {  
    28.   
    29.                 //可以直接对Page对象进行操作  
    30.   
    31.                 ((Page)handler).PreLoad += new EventHandler(MyHandlerFactory_PreLoad);  
    32.   
    33.             }  
    34.   
    35.         }  
    36.   
    37.         void MyHandlerFactory_PreLoad(object sender, EventArgs e)  
    38.   
    39.         {  
    40.   
    41.             ((Page)sender).Response.Write("Copyright @Gspring<br/>");  
    42.   
    43.         }  
    44.   
    45.         public void ReleaseHandler(IHttpHandler handler)  
    46.   
    47.         {  
    48.   
    49.         }  
    50.   
    51.     }  

    web.config中配置:

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

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

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

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

    view plaincopy to clipboardprint?

    <httpHandlers>     

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

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

  • 相关阅读:
    linux上安装vsftpd
    springboot集成mybatisplus
    springboot集成swagger2
    ssm+maven多模块项目整合
    追梦强人工智能(一)
    Linux环境kafka安装
    Linux环境安装jdk10
    东芝笔记本Satellite M40-A
    Maven简介
    postgresql PL/pgSQL—存储过程结构和变量声明
  • 原文地址:https://www.cnblogs.com/zhangxiaolei521/p/4937614.html
Copyright © 2011-2022 走看看