第一步: 创建一个类, 并继承: IHttpModule
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.web;
- namespace ThreeLayerWebDemo._2019_7_14_Event
- {
- public class MyHttpModule : IHttpModule
- {
- public void Dispose()
- {
- }
- public void Init(HttpApplication context)
- {
- // 注册第一个 HttpApplication 第一个事件
- context.BeginRequest += Context_BeginRequest;
- }
- private void Context_BeginRequest(object sender, EventArgs e)
- {
- var App = sender as HttpApplication;
- App.Response.Write("这是来自 HttpModuel 的代码 < br/>");
- }
- }
- }
第二步: 配置 Web.config 文件, 分别在 system.Web 和 system.webserver 下添加以下节点, type 的值为: 类的命名空间 + 类名
- <system.Web>
- <httpModules>
- <add name="DemoModule" type="ThreeLayerWebDemo._2019_7_14_Event.MyHttpModule"/>
- </httpModules>
- </system.Web>
- <system.webServer>
- <modules>
- <add name="DemoModule" type="ThreeLayerWebDemo._2019_7_14_Event.MyHttpModule"/>
- </modules>
- </system.webServer>
ThreeLayerWebDemo._2019_7_14_Event.MyHttpModule: 如下图
测试
搞定~
来源: http://www.bubuko.com/infodetail-3122978.html