自定义处理页面请求

来源:互联网 发布:淘宝 美美歌家代购 编辑:程序博客网 时间:2024/05/21 13:58

主要是继承IHttpModule和IHttpHandler来重写其 中的方法,IhttpModule 中重写其Init方法及各种请示过程事件和Dispose方法

using System.Web;
using System;

namespace CustomerHttpModules
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>

    public class MyHttpModules:IHttpModule
    
{
        
public MyHttpModules()
        
{
             
        }

        
IHttpModule 成员
    }

}

 

然后生成dll,新建个asp.net应用,引用此dll,在web.config中加入

<httpModules>
  <add name="test" type="CustomerHttpModules.MyHttpModules,CustomerHttpModules"/>
 </httpModules>

则当有页面请求时会查找到此dll,然后执行其中过程。

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

以下为重写IHttpHandler中方法的代码:

同样地建个类库

using System;
using System.Web;

namespace CustomerHttpHandler
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>

    public class TestCustomerHttpHandler:IHttpHandler
    
{
        
public TestCustomerHttpHandler()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
IHttpHandler 成员
    }

}

所有页面请示的核心处理都是通过IHttpHandler的ProcessRequest方法来完成,因此只要我们重写此方法,则无论页面有任何请求,都会被我们所重写的内容代替掉。嘿嘿

生成dll后再在asp.net应用中的 web.config 中加入

<httpHandlers>
  <add verb="*" path="*" type="CustomerHttpModules.MyHttpModules,CustomerHttpModules"/>
 </httpHandlers>

看看效果吧

 

原创粉丝点击