2021-05-31

HttpModule对象

HttpModule的概述和使用
对于HTTP请求而言,HttpModule是HTTP请求的必经之路,能在HTTP请求传递到最终的“请求”做处理之前进行一些额外的工作…

通过一个简单的Web应用来说明HttpModule的用法
首先创建一个网站,然后添加两个ASP.NET页面

1,创建一个HttpModule类

namespace Exampele1
{
	public class HttpModule : IHttpModule
    {
    	public void Dispose()
        {
            throw new NotImplementedException();
        }
        public void Init(HttpApplication context)
        {
            //初始化,为HttpApplication绑定两个事件
      	    //HttpHandle处理前的事件
          context.BeginRequest += Context_BeginRequest;
            //HttpHandle处理后的事件
          context.EndRequest += Context_EndRequest;
        }
	  //在处理之前附加信息
      private void Context_BeginRequest(object sender, EventArgs e)
      {
          HttpApplication app = sender as HttpApplication;
          application.Response.Write("开始处理请求");
      }
      //在处理之后附加信息
      private void Context_EndRequest(object sender, EventArgs e)
      {
          HttpApplication app = sender as HttpApplication;
          application.Response.Write("结束处理请求");
      }
    }
}

2,配置配置信息,实现了HttpModule类,还要在web.config中配置

<system.webServer>
	<modules>
		<add name="test" type="Example1.TestHttpMoudle"/>
	</modules>
</system.webServer>

3、在创建的两个asp.net页面的源文件中,分别在各自的标签下添加
第一个:欢迎来到页面1
第二个:欢迎来到页面2

最后运行一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值