.net 计算页面的执行时间

使用IHttpHandler来实现对页面执行时间的统计:

IHttpHandler:用处理请求的接口,可以在web.config下的HttpHandler中设置对应哪种请求类型(GET/POST),请求所在的路径(Path),请求的扩展名(verb)

<add verb="*" path="*.*" type="xx.t" />

IRequiresSessionState:你要使用Session就要继承这个接口

namespace xx{

public class t: IHttpHandler, IRequiresSessionState
  {
    public void ProcessRequest(HttpContext context)
    {
      string rawUrl = context.Request.RawUrl;
      DateTime startTime = DateTime.Now;      
      string aspxPagePath = rawUrl.Substring(0, rawUrl.IndexOf(".aspx") + 5);
      IHttpHandler handler = PageParser.GetCompiledPageInstance(aspxPagePath, null, context);//执行请求的页面
      // Process the page just like any other aspx page
      handler.ProcessRequest(context);
      TimeSpan duration = DateTime.Now - startTime;
      context.Response.Write(String.Format("Request finshed. Total duration: {0} ms.",
        duration.Milliseconds));
    }
  
    /// <summary>
    ///
    /// </summary>
    public bool IsReusable
    {
      get { return true; }
    }
    #endregion
  }
}

}

posted on 2011-04-28 18:18  lock  阅读(354)  评论(0编辑  收藏  举报