EventArgs e){HttpApplication application = sender as HttpAp
在之前的文章介绍过HttpModule,在这就不烦琐了。
今天完成了一个小案例,效果如图:
为原有的文本,添加一些其它信息,实现思路如下:
一、创建TestHttpModule类,并实现IHttpModule接口。
二、实现IHttpModule接口中的要领,为HttpApplication东西的BeginRequest事件绑定要领,实此刻用HttpHandler措置惩罚惩罚每个请求前,附加特别信息的成果。
namespace HttpModule_Demo.App_Code { public class TestHttpModule : IHttpModule { public void Dispose() { } 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>"); } } }
三、在web.config中配置TestHttpModule类。
<!--配置节点--> <system.webServer> <modules> <!--type="定名空间.类名"--> <add name="test" type="HttpModule_Demo.App_Code.TestHttpModule"/> </modules> </system.webServer>
四、创建一个aspx页面,并添加页面内容。
<body> <form id="form1" runat="server"> <div> <h2>欢迎访谒页面1</h2> </div> </form> </body>
五、分袂测试两个aspx页面,不雅察看页面输出的内容。
案例很简单,但是要理解它的流程。
此中有个小常识点,可以和大家分享下:
TestHttpModule实现IHttpModule接口时,它实现了二个要领,一个是Dispose,另一个是Init初始化要领。,下面的一段中“+=”小我私家理解,其实是一种多播委托,把Event事件连接在一起执行。
context.BeginRequest += Context_BeginRequest; context.EndRequest += Context_EndRequest;
选择TestHttpModule类中的关键字BeginRequest并且转到界说(F12),,会看到如下的代码。
asp.net 常识回顾(三)
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32090.html