HttpModule的使用

1:创建TestHttpModule类:继承于接口IHttpModule

namespace WebApplication5._1
{
    public class TestHttpModule : IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += context_BeginRequest;
            context.EndRequest += context_EndRequest;
        }

        private void context_EndRequest(object sender, EventArgs e)  //在页面运行时输出
        {
            HttpApplication application = sender as HttpApplication;
            application.Response.Write("<p>HttpModule开始处理请求</p>");
        }

        private void context_BeginRequest(object sender, EventArgs e) //在页面结束后输出
        {
            HttpApplication application = sender as HttpApplication;
            application.Response.Write("<p>HttpModule结束处理请求</p>");
        }

    }
}

2:web.config配置文件在<configuration>下添加

<system.webServer>
        <modules>
            <add name="TestHttpModule" type="WebApplication5._1.TestHttpModule"/> //指向WebApplication5方案名称下的TestHttpModule类(WebApplication5._1.TestHttpModule)
</modules> </system.webServer>

 

posted on 2022-04-19 18:08  钟硕cool  阅读(55)  评论(0编辑  收藏  举报

导航