在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
最近对页面的生命周期比较感兴趣,看了一些牛人们写的文章,感觉似懂非懂(仍需要继续努力^_^). 大致的执行过程是这样的:client端发送页面请求,被IIS的某个进程截获,它根据申请的页面后缀(.aspx)不同,调用不同的页面处理程序(.asp->asp.dll; .aspx->ISAPI.dll).而页面处理程序在处理过程中,则要经历HttpModule,HttpHandler的处理:前者用于页面处理前和处理后的一些事件的处理,后者进行真正的页面的处理。
//Class: HttpModuleTest.cs
//--------------------------------------------- using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// HttpModuleTest 的摘要描述 /// </summary> public class HttpModuleTest:IHttpModule { //Init方法用来注册HttpApplication 事件。 public void Init(HttpApplication application) { application.BeginRequest += (new EventHandler(this.Application_BeginRequest)); application.EndRequest += (new EventHandler(this.Application_EndRequest)); } //在页面处理前执行这个事件方法 private void Application_BeginRequest(Object source, EventArgs e) { HttpApplication application = (HttpApplication)source; HttpContext context = application.Context; context.Response.Write("<h1><font color=red> HelloWorldModule: Beginning of Request</font></h1><hr>"); context.Response.Write("<a href='http://www.baidu.com'><h2><font color=blue>click here you will see some ads</font></h2></a>"); } //在页面处理前执行这个事件方法 private void Application_EndRequest(Object source, EventArgs e) { HttpApplication application = (HttpApplication)source; HttpContext context = application.Context; context.Response.Write("<hr><h1><font color=red>HelloWorldModule: End of Request</font></h1>"); } //资源释放 public void Dispose() { } } //-----------------------------------------------
<httpModules >
<add name="test" type ="HttpModuleTest,HttpModuleTest"/> </httpModules>
public class IpFilterModule:IHttpModule
{ public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); } void context_BeginRequest(object sender, EventArgs e) { //在事件处理过程中,我们可以获得当前请求的上下文,根据请求的上下文,我们可以对 //请求执行任何操作 HttpApplication app = sender as HttpApplication; //获得当前应用程序 if (app != null) { //app.Context 获得当前上下文信息 string hostname = app.Context.Request.UserHostName; if (hostname!="cc") { app.Context.Response.StatusCode = 404; app.Context.Response.End(); } } } public void Dispose() { } }
|
请发表评论