-------Web.config----------------
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpModules>
<add name="myModule" type="模块与处理程序.MyModule"/>
</httpModules>
</system.web>
</configuration>
---------MyModule.cs类-------------------
namespace 模块与处理程序
{
public class MyModule:IHttpModule
{
public void Dispose()
{
//没有要释放的资源就什么也不写
}
HttpContext _current = null;
public void Init(HttpApplication context)
{
this._current=context.Context;
context.BeginRequest += new EventHandler(context_BeginRequest);
context.EndRequest += new EventHandler(context_EndRequest);
}
void context_EndRequest(object sender, EventArgs e)
{
_current.Response.Write("<!--Request end-->");
}
void context_BeginRequest(object sender, EventArgs e)
{
_current.Response.Write("<!--Request begin-->");
}
}
}
-------------default1.aspx前台:---------------------
<body>
<form id="form1" runat="server">
<div>
hello very one,it's nice to meet your here!
</div>
</form>
</body>
本文介绍了一个具体的ASP.NET HttpModule实现案例,展示了如何通过自定义HttpModule来监控HTTP请求的开始和结束,并在请求周期中插入自定义的行为。具体包括在Web.config文件中的配置方式,以及对应的C#代码实现细节。
340

被折叠的 条评论
为什么被折叠?



