有没有一种简单的方法可以获得ASP.Net中的总页面响应时间?

Ear*_*rlz 3

通常说PHP或其他Web框架获得总响应时间很简单,只需启动文件顶部的计时器并在结束时停止它.

在ASP.Net中有整个页面生命周期位,但我不知道如何做到这一点.我希望此响应时间记录在母版页中进行,响应时间显示在页脚的页脚中.这样做的最佳方式是什么?是否内置了ASP.Net的内容?是否可以包含OnRender时间?

ale*_*bog 5

你可以在Global.asax上做,看一下这篇文章

void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Items["renderStartTime"] = DateTime.Now;
}

void Application_EndRequest(object sender, EventArgs e)
{
DateTime start = (DateTime) HttpContext.Current.Items["renderStartTime"];
TimeSpan renderTime = DateTime.Now - start;
HttpContext.Current.Response.Write("<!-- Render Time: " + renderTime + " -->");
}
Run Code Online (Sandbox Code Playgroud)

  • 不需要在Application_BeginRequest中设置项目索引。在Application_EndRequest中,您可以只引用HttpContext.Current.Timestamp。该值是在创建 HttpContext 对象时由框架设置的。在测试中,该值与上述过程相同。 (3认同)