Render timing-data for MVC requests

If you want the simplest way to compute how long your page render times are – and display them on each ASP-MVC page you render, then look no further!

Place this i Global.asax.cs (this is really weird, but apparantly the mere fact that the method exists makes MVC call it – you don’t need to register it or override anything. Bad form, I say)

protected void Application_BeginRequest()
{
HttpContext.Current.Items["stopwatch"] = Stopwatch.StartNew();
}

And then add this to your view (_Footer.cshml, if you’ve got it)

@(string.Format("{0:0.000} s",((System.Diagnostics.Stopwatch)HttpContext.Current.Items["stopwatch"]).ElapsedMilliseconds/1000.0))
Advertisements