经过一段时间的摸索,终于找到了一个完全解决方案:
1.在工程中添加一个类,就叫做PageElapse.cs
代码如下(微软PM&A2K讲座的范例代码):
using System;
using System.Web;
namespace Common
{
/// <summary>
/// PageElapse 的摘要说明。
/// </summary>
public class PageElapse : IHttpModule
{
private DateTime _StartTime;
public void Init(System.Web.HttpApplication context)
{
context.BeginRequest += new EventHandler(OnBeginRequest);
context.EndRequest += new EventHandler(OnEndRequest);
}
public void Dispose()
{
}
private void OnBeginRequest(object sender, EventArgs e)
{
_StartTime = DateTime.Now;
}
private void OnEndRequest(object sender, EventArgs e)
{
TimeSpan ts = DateTime.Now - _StartTime;
HttpContext.Current.Response.Write("<XML ID=PageElapseTime><Time><ElapseTime>" + ts.TotalMilliseconds + "ms</ElapseTime></Time></XML>");
}
}
}
2.在web.config的<system.web></system.web>中添加:
<httpModules>
<add type="Common.PageElapse, Common name="Common" />
</httpModules>
3.在要现实执行时间的页面中添加:
<div datasrc=#PageElapseTime datafld=ElapseTime>
这里使用数据岛输出,其实所有的页面中都会有"<XML ID=PageElapseTime><Time><ElapseTime>xxxxms</ElapseTime></Time></XML>",但是只有加了"<div datasrc=#PageElapseTime datafld=ElapseTime>"的页面才会显示出来