... my attempt at documenting my adventures as a developer.
I regularly come across, or generally do quite interesting things in my day job, so when I have some time I try and write about them. Hopefully you'll find some of the Articles handy.
The primary reason I started this web site was to share with you the things I come across in my day job as a Web Developer, the first batch of articles I am going to write will be around the tools I find invaluable in my role.
So first and foremost, let’s take a look at MiniProfiler.
Whilst in your development environment you can use it to get an overlay on your page detailing the code execution of your Application. For those of you that have ever tried to identify bottlenecks in your applications you can immediately see how useful this can be. Take a look at this screenshot from the Jambr blog page.
As you can see, the steps needed to render my page, and most importantly the SQL that was executed in the process are clearly detailed out here, you can even click on the query to view the detail, for example:
I’ll go into all of this a little more in later articles, but for now let’s look at setting up and configuring MiniProfiler against a .NET 4.5 MVC 4 Application which uses Entity Framework 5 for its database connections.
Installation is simple and is all done through the Package Manager Console integrated into Visual Studio.
MiniProfilerEF.Initialize_EF42()This ensures that any connections created and used by Entity Framework are captured and logged into MiniProfiler like in my screenshot above.
Almost done, I promise. We now need to add a piece of code to the page you want to see the profiler on. Personally I have this on my Base.Master, the parent master page for all others, that way it’s available on every page. The code you need to add, in your <head> tag is:
You may need to add a reference to the namespace at the top of your page like so:
<%@ Import Namespace="StackExchange.Profiling" %>
Run your application and see what happens…
"Oh wait, Karl, you lied – it doesn’t work and I’m getting the following error when running MVC4"
Failed to load resource: the server responded with a status of 404 (Not Found) http://*/mini-profiler-resources/results
This article wouldn’t be interesting unless it solved at least one annoyance now, would it? To fix the above error you need to add the following to your Web.Config
<system.webServer> <handlers> <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
This sorts out the routing issue which appeared in MVC4. Now try again, and you’ll be sorted!
MiniProfiler is a very powerful tool, I will go into the more advanced features of it in a later Article.