自定义处理页面请求

 

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

None.gif using  System.Web;
None.gif
using  System;
None.gif
None.gif
namespace  CustomerHttpModules
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Class1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class MyHttpModules:IHttpModule
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public MyHttpModules()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif             
ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
IHttpModule 成员#region IHttpModule 成员
InBlock.gif
InBlock.gif        
void System.Web.IHttpModule.Init(HttpApplication context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            context.BeginRequest
+=new EventHandler(context_BeginRequest);
InBlock.gif            context.EndRequest 
+= new EventHandler(this.context_EndRequest);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void context_BeginRequest(object obj,System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HttpApplication app 
= (HttpApplication)obj;
InBlock.gif            app.Response.Write(
"Hi,testing customer http module request");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void context_EndRequest(object obj,System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HttpApplication app
=(HttpApplication)obj;
InBlock.gif            app.Response.Write(
"Hi,end request here");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// TODO:  添加 MyHttpModules.Dispose 实现
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

 

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

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

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

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

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

同样地建个类库

None.gif using  System;
None.gif
using  System.Web;
None.gif
None.gif
namespace  CustomerHttpHandler
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Class1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>
InBlock.gif    public class TestCustomerHttpHandler:IHttpHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public TestCustomerHttpHandler()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
IHttpHandler 成员#region IHttpHandler 成员
InBlock.gif
InBlock.gif        
public void ProcessRequest(HttpContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif             context.Response.Write(
"hi,I write something instead of what you input");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool IsReusable
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// TODO:  添加 TestCustomerHttpHandler.IsReusable getter 实现
InBlock.gif
                return true;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

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

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

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

看看效果吧

转载于:https://www.cnblogs.com/jabby12/archive/2004/12/28/83244.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值