如何显示在页脚中生成页面所需的持续时间?

Sam*_*ron 6 asp.net asp.net-mvc asp.net-mvc-2

在调试版本期间,我想展示服务器端在页脚中生成页面所花费的持续时间.

因此,例如,如果页面需要250ms服务器端,我希望在页脚中显示,在调试版本中.如何在ASP.NET MVC项目中实现此目的?

Mar*_*len 5

将其添加到母版页中的页脚:

Page rendering took <%= DateTime.Now.Subtract( this.ViewContext.HttpContext.Timestamp ).TotalMilliseconds.ToString() %>

您还可以将其包装在扩展方法中:

public static class Extensions
{
  public static string RequestDurationinMs( this HtmlHelper helper )
  {
    #if DEBUG
    return DateTime.Now.Subtract( helper.ViewContext.HttpContext.Timestamp ).TotalMilliseconds.ToString();
    #endif
  }
}
Run Code Online (Sandbox Code Playgroud)

像这样使用:

<%= Html.RequestDurationinMs() %>
Run Code Online (Sandbox Code Playgroud)

您可能需要导入扩展类的命名空间: <%@ Import Namespace="Your.Namespace" %>