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))

About mfagerlund
Writes code in my sleep - and sometimes it even compiles!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: