Want to see the http requests your asp.net code is making?

A lot of projects I work on involve integrating with a third party API. It could be as simple as a few calls to enhance a section, or complete reliance on an API to provide the backbone for an application.

However deep your integration, it is vital to understand what happening as calls are made from your application to an API. Without the detail of the requests, the contents of each request, and the repsonse provided by the API, your development time will be considerably increased. It’s the same as trying to debug an application by just ‘console.logging’ variable values. Without better tools, you’re going to be there a long time.

Tools like Fiddler or Charles will show calls between the front and back end of your applciation, but once it leaves the back end of your asp.net application, the request disappears in to a a black hole.

After struggling with this for many months I found a Stack Overflow post (of course) which suggested adding network tracing via the web.config to log requests and responses. So, by adding the following magic to the web.config

    <trace autoflush="true" /> 
        <source name="System.Net" maxdatasize="1024">
                <add name="VisualStudioConsole"/>
            <add name="VisualStudioConsole" type="System.Diagnostics.ConsoleTraceListener" />
        <add name="System.Net" value="Verbose" />

The following data is logged to the Output window in Visual Studio….

System.Net Verbose: 0 : [9712] 00000000 : 7B 22 4D 65 6D 62 65 72-4E 75 6D 62 65 72 22 3A : {"UserNumber":
System.Net Verbose: 0 : [9712] 00000010 : 34 34 34 37 37 2C 22 43-61 6C 63 75 6C 61 74 69 : 44477,"Calculati
System.Net Verbose: 0 : [9712] 00000020 : 6F 6E 4E 61 6D 65 22 3A-22 4C 45 41 56 45 52 51 : onName":"LEAVEQ
System.Net Verbose: 0 : [9712] 00000030 : 55 4F 54 45 22 2C 22 50-61 72 61 6D 65 74 65 72 : UOTE","Parameter
System.Net Verbose: 0 : [9712] 00000040 : 73 22 3A 5B 7B 22 50 61-72 61 6D 65 74 65 72 4E : s":[{"ParameterN
System.Net Verbose: 0 : [9712] 00000050 : 61 6D 65 22 3A 22 43 41-4C 43 44 22 2C 22 50 61 : ame":"CALCD","Pa
System.Net Verbose: 0 : [9712] 00000060 : 72 61 6D 65 74 65 72 56-61 6C 75 65 22 3A 22 30 : rameterValue":"0
System.Net Verbose: 0 : [9712] 00000070 : 36 2F 30 31 2F 32 30 31-36 22 7D 5D 7D          : 6/01/2016"}]}

The configuration added to the web.config above creates a listener to System.Net. This logs some public methods of the HttpWebRequest and HttpWebResponse classes (amongst others). Full details are available on MSDN for configuring network tracing in your application.

The output above shows the body of a request that was sent to an api – in this case a json object. In amongst all that noise, you can just about see some curly braces. Although the output format is not ideal, it does provide the essential visibility that did not exist before.

This is a quick and dirty way to start logging the API requests your application is making. If anyone as anything better, please add a comment below.

Configuring Umbraco 7 for MiniProfiler

This post is based on the following Umbraco setup.
  • Umbraco v7.2
  • Azure Sql Server – Web Edition

Why create this post.

During a previous v7.2 Umbraco project I tried several times and failed to configure Umbraco to use Mini Profiler. This seemed the simplest route to debugging some performance problems I was having with the site. However, there was no definitive guide out there on how to configure Mini Profiler that worked for me. So, to try and create that guide, and to save me searching across multiple blog posts and forums each time, I’ve created this,

What is Mini Profiler

Mini Profiler is a component from those clever bods over at Stack Exchange, and has been part of Umbraco since vx.xx When enabled, Mini Profiler provides diagnostics about your application in a

How to configure in Umbraco 7.

1. Add the following piece of Razor to the views you want to debug.
2. Add the following using statement to the top of all views
using StackExchange.Profilling

3. Ensure the debug attribute of the compilation element in the web.config in the root is set to true.

4. Open the page you want to debug in the a web browser and append ?umbDebug=true to the query string.

And that is it!

Filtering nodes in the Umbraco back office

This post is based on the following Umbraco setup.

  • Umbraco v7.2
  • Azure Sql Server – Web Edition


Is there a way in the Umbraco back office to selectively filter tree nodes based on a property value on the user?


Sadly, no. There is currently nothing natively in the back office which will allow the conditional display of tree nodes. However using Umbraco’s event model and API, it is fairly simple to add this in.


In this particular Umbraco installation, each back office user will be assigned to a country. In one area of the content tree, each node has a country property. The tree structure looked like this;

– German Answer 1
– French Answer 1
– USA Answer 1
– Australia answer 1
– French answer 1

If a French back office user logged in to the back office he should only see this;

– French answer 1
– French answer 2

The Solution

Of course I can take no real credit for this solution. Once again the excellent resource that is Our Umbraco provided the answers. I was aware that Umbraco used an event model, so the first piece of the puzzle was understanding which event to hook on to. The answer event I was looking for was;


I created a TreeEvents class which inherited from ApplicationEventHandler. I then overrode the ApplicationStarted method to which I added the following event handler;

protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) 
 TreeControllerBase.TreeNodesRendering += FilterCountryNodes; 

The handler looked like this;

public static void FilterCountryNodes(TreeControllerBase sender, TreeNodesRenderingEventArgs eventArguments)
  if (sender.TreeAlias == Constants.UmbracoNative.CmsSections.ContentAlias)
    var currentUserLevel = sender.Security.CurrentUser.UserType.Alias;
    if (currentUserLevel == Constants.ContentWorkflow.UserTypes.LowLevel)
      var answerNodeIds = eventArguments.Nodes.Select(n => Int32.Parse(n.Id.ToString()));
      var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
      var allCountries = umbracoHelper.TypedContentAtXPath(Constants.XPaths.GlobeSearchCountries);

      var countriesUserCanEdit = TreeHelper.GetCountryIdsUserCanEdit(allCountries, sender.Security.CurrentUser.Id);

      var answers = sender.ApplicationContext.Services.ContentService.GetByIds(answerNodeIds);
      var nodesToRemove = TreeHelper.GetNodesToRemove(answers, countriesUserCanEdit);
      eventArguments.Nodes.RemoveAll(node => nodesToRemove.Contains(Int32.Parse(node.Id.ToString())));

To break this down a litle, the method firstly only acts if the type of Umbraco tree we are receiving the event from is the Content section. A tree in this case is considered to be any one of the Cms sections eg Content, Media, etc, including any custom sections you may have added. We are also only performing this filtering for a particular back office user type (LowLevel), so we inspect the user type alias property to make sure the logged in user is of the correct type.

The final piece of the puzzle was trying to associate a user to a country, and filtering the answers by the user’s countries. I started by trying to add countries to a back office user. However, unlike all other areas of Umbraco, the Users section and Users doesn’t appear to be extensible in any way, so another forum post in “Our” helped turn the problem on its head.

Adding users to a country

In Umbraco I have a Country document type defined. To this I added a new multi user picker property. This allowed me to associate multiple Umbraco back office users with a particular country.

In my event handler I check the logged in user has been added to the new property for each country.

var allCountries = umbracoHelper.TypedContentAtXPath(Constants.XPaths.SearchCountries);
var countriesUserCanEdit = TreeHelper.GetCountryIdsUserCanEdit(allCountries, sender.Security.CurrentUser.Id);

Once we have a list of the logged in user’s countries, we then retrieve the answers for all countries. We then find any answers which don’t contain at least one of the users countries – a user can be associated with multiple countries. Finally once we have a list of answers which the logged in user shouldn’t be able to view, we call the Umbraco API method RemoveAll()

var answers = sender.ApplicationContext.Services.ContentService.GetByIds(answerNodeIds); 
var nodesToRemove = TreeHelper.GetNodesToRemove(answers, countriesUserCanEdit); 
eventArguments.Nodes.RemoveAll(node => nodesToRemove.Contains(Int32.Parse(node.Id.ToString())));

And that’s it!


My only reservation with this approach is that it may take some explaining to content editors. Restricting access to all other areas of Umbraco is controlled via the Members section. So this muddies the waters in terms of which areas of the back office perform which function. However I was unable to find another way. If anyone has any pointers to improve this, then please comment below.

MVC Controller Action called multiple times per page load.

Today I came across a real time sink of a gotcha with my MVC site. So I thought I’d share this on a post.

The issue
A standard Mvc controller action was getting called multiple times each time a GET request was made in the browser. Sometimes twice, sometimes once, and occasionally more.

Opening up Chrome dev tools to try and understand the problem, I spotted the multiple requests in the Network tab. Interstingly when I filtered by type the additional requests were showing up in the Images tab. Looking at the media type, the requests were being made the following acceptt header


The initiator column pointed to javascript being the culprit.


However, this was a red herring. After a lot of code removal, view in browser, rinsing and repeating, I isolated it to the following line

<div id="hero" style="background-image: url('@Model')">

The cause
It took a while for the penny to drop, but the problem occurred when the Model was an empty string. So with nothing passed to the url property, the background image was requesting controller once the markup rendered. This was happening in a number of places in the page. So the controller was being called as many as four times each time the page was loaded.

I only tested this in Chrome, so the behaviour may not be the same across a other browsers.

The solution
As this markup was in a number of places across the site, I created an Html Helper extension, which simply checks to see if the string is null or empty.

My markup now looks like this

<section class="pageHero compactHero bottomAligned" @Html.StyleAttributeForHeroImage(Model.BackgroundImage)>

And the Helper looks like this;

public static class StyleAttributeExtensions
    public static MvcHtmlString StyleAttributeForHeroImage(this HtmlHelper helper, string imageUrl)
        var styleAttribute = String.Format("style=\"background-image: url('{0}')\"", imageUrl);

        if (String.IsNullOrWhiteSpace(imageUrl))
            styleAttribute = String.Empty;
        return MvcHtmlString.Create(styleAttribute);


So now I only have one request per controller action when each pages loads. This has resulted in a noticeable improvement in page load times.

Hope this helps someone else out.

Umbraco Preview: Are my changes live or not?

This post is based on the following Umbraco setup.

  • Umbraco v7.2
  • Azure Sql Server – Web Edition


Preview in Umbraco lets you quickly see how your page will look on different devices. It allows content editors to switch between different devices previews and see how their content will display on a number of common device sized before making their changes live. Recently I had a client raise a few concerns about how the preview functionality worked and so this post is for all those who have to do the same in the future.
Where’s my preview button?

The first problem the client had was not being able to see a preview button on their newly created pages. In order to preview content, the content must first be saved. Once saved (but not necessarily published) the preview button will appear in Umbraco.

Is it live or not!

When a content editor clicks preview they are able to see what the website will look like before it is published and available to all website members. Umbraco does this by setting a cookie in the user’s browser which is persisted until they log out of the CMS. This caused great confusion with my client who was concerned the preview functionality was broken. They had made changes and previewed them in the cms. They had then visited the main site as a member, while still logged in to the CMS. As the preview cookie had not been cleared they could see their unpublished changes on the live site. Of course the changes were not live, and once they logged out of Umbraco, normal service was resumed. However, we now always advise….

Once content editors have finished previewing content, they should always logout of Umbraco to ensure they are seeing live content rather than preview content.

Talking with tech leads: James Gaisford

At the end of last year I was offered the role of Head of development at my company. Having been a reluctant senior developer, I was a little reluctant to take the role, but after some careful consideration I accepted the offer. As with previous career challenges, I sought advice and guidance from the internet. But the best steer came from an unlikely source – a tweet from Martin Fowler

The book referred to in the tweet is called “Talking with tech leads” and it is a must read for all developers thrust into the strange and unfamiliar world of leading a technical team. The book takes the form of interviews with a number of tech leads, asking a core set of questions particularly around significant challenges faced and how they balance the technical and non-technical worlds.

So, after my first six months as a tech lead, I present my answers to those same core questions asked in the book.

What should a Tech Lead focus on and why?

Solving problems without coding. As a developer your core skill is to be able to solve problems by coding. This is a skill that has been honed over many years. It is very tempting as a developer, new to the tech lead role, to offer only code based solutions to customer problems, but stop. and think if there could be a non-code based solution to the problem presented. Pushing back on client requirements? Negotiating with project managers to get additional resource? Scrutinizing client requirement documents? Writing tight functional specifications. All of these and other non-code based solutions can come to your rescue. Expand you toolbox.

What has been your biggest challenge as a Tech Lead?

Don’t over-commit…. and delegate. With a new role comes a lot of new responsibility. This means a lot more work. Depending on the set up in your company, there may be an expectation that you still perform some or all of your previous role’s responsibilities as well. You will be asked to do a lot, but don’t be afraid to push back, to say no. You may have a team to whom you can delegate some of your work to. Don’t be afraid to use them. No one will think you are incapable, slacking off, or being lazy. In fact, if the team is good, they will relish the challenge…. just don’t over do it.

Any time-management tips?

Get in early. Process emails. Work out a plan of attack for the day. Create a simple list using a pen and paper – I know, it’s pretty revolutionary – in priority order to work through. Close the inbox. Cross through items on your list. Its amazing how that simple process can help feel like you are “winning” t the day. Decline any meetings invites that aren’t relevant. And for those meetings you have to go to, make sure there is an agenda with a clear purpose, and any actions noted.

How do you strike the right balance between writing code and dealing with other issues?

This is really tough. The thing that makes this easier, is knowing you have a team of people who are as good, if not better, than you at coding. So, there is no need to worry that the code will get written. Of course you should always try and devote at least some time in your week to writing code, but nothing that will take longer than a day and nothing that would jeopardise the project if you had to drop it…. which you most likely will.

Getting started with Dialogue for Umbraco

Following a great talk at the 2014 UK Umbraco Festival by Lee Messenger from Aptitude I’ve was inspired to try Dialogue. Dialogue is an forum package for Umbraco 7 based on MVCForum. The current version is a port into Umbraco, with plans to add new features and functionality as the package evolves. This post details the experience I had trying to get up and running with Dialogue and the pitfalls I encountered.
My first attempt at an install went fine……at first. When I spun the application up in the browser however I encountered the error message
The specified table does not exist [DialogueTopic]
Turns out I had furiously clicked all the defaults when installing Umbraco and was running the site against Sql CE rather than SQL Express. The package has only been tested with Sql Server Express and Sql CE is not supported as detailed here
No post for me?
Once over that hurdle, the next issue I encountered was being unable to create a discussion, or post…… or topic (the UI is a little ambiguous about it’s terms). No matter how much I searched I could not find a setting in the default forum created by the package. No settings in the forum to enable posting. No settings against the member. This was all the UI presented;

……..and so, to the code
Turns out the display of the “create topic” button is controlled by the following clause in one of the views;
@if (Model.LoggedOnUser != null && Model.LoggedOnUser.DisablePosting != true && Model.UserCanPostTopics)
The test user I created met the first two conditions, but UserCanPostTopics was set to false. A quick post in the Umbraco community forum and a swift response from Dan Lister pointed out that the UserCanPostTopics setting is set against the category.

Each category allows for fine grained permissions. Each member group can have distinct permissions, as you can just about see in the screenshot above. One of these permissions is “Create Topics”. Once enabled on the “Dialogue Standard” group the button appeared in the UI and I could create topics. As a suggestion, it might be worth adding this permission by default to the initial category created by the install. 
So far, the package looks great and I’m looking forward to exploring it further in te ecoming weeks.

Paper prototyping HTML layout


Paper prototyping is good. It saves you time. So why not extend the principle to other areas of your build, like your layout, and save more time?

On the last few projects at work we have trialled paper prototyping. For the uninitiated, paper prototyping involves using pencils and paper to sketch out user interface ideas and quickly iterate on those ideas, getting closer to the final UI much earlier in the project. On the whole it has worked very well, and from a developer perspective a lot of UI issues which often only get considered during the build are resolved before a single development key has been stroked.

So on our latest project, I brought the concept of paper prototyping to another area of development which, like UI, can be ambiguous, contentious and time consuming. The HTML structure of a site. This post is a quick how to for paper-prototyping your next HTML layout.

How to paper prototype your HTML


  1. Print outs of key pages of the final site design.
  2. Tracing paper
    – If speaking to a designer ask for a “layout pad” for added kudos
  3. A lightbox (not required). 
  4. More than one developer.


1. Place your printed site design on the lightbox.
2. Place a sheet from your layout pad over the top of your site design.
3. Now start sketching and debating your proposed layouts with your fellow devs.
4. Rinse and repeat.
Sounds simple? It is. A fellow developer and I sat over the lightbox, drawing out our ideas, suggesting layouts and annotating them with simple style attributes to remind us of our initial thoughts on CSS when we came to develop the front end, for example, “float this left”, “container with fixed width”. Some ideas were quickly dismissed, while others were explore. Some even made it in to the final layout. In under half an hour we had a pretty solid layout which would have taken possibly half a day (if not longer) with just one of us sat in Visual Studio. 
Here are some pictures showing the paper prototypes of the layout my colleague and I created as we thought through and discussed the available options for a recent project. 
Attempt 1
Attempt 2
Attempt 3 with design print out beneath.

Ok, but what’s in it for me?

This really was just an experiment to see if we could extend the principles of paper prototyping to other areas of the development process to reduce the time spent developing. 
By getting out from behind the keyboard, picking up a pencil and discussing ideas, we got to a solid layout in a comparatively short space of time, collaborating and sharing ideas along the way. 
I would urge other developers to give it a try. Even without the lightbox, just sit around the table and discuss how you would lay a site out. You may even save time and produce a finished site quicker.
A testement to this methods success is that since comitting the initial paper protoptype in to the project’s MVC layout, it has only been altered a few times. 

Creating a better developer test

Hate is a strong word. Its a word that should be used sparingly. A word only wheeled out in times of desperation. A word reserved for the things in life you would prefer never to see again.

I hate developer tests. Let me be unequivocal here for a moment. I really hate developer tests. As a developer of 13 or so years I have sat my fair share of these things, some better than others, but I doubt any of them were the deciding factor in me getting the job or not.

Haters gonna hate

One of my main gripes is that most developer tests prove nothing. 

“What is the name of the Nuget scaffolding package for Asp.Net MVC3?”

“List the events in page life cycle.”
“List all templates of the Repeater control”

If you can answer the three questions above, congratulations, you can obviously Google with the best of them. Every developer test will have a few questions like those above. Questions like this prove nothing. So you can remember that there is a SeparatorTemplate in a repeater. What has this told your prospective employer? Precisely nothing. Having Google has abstracted away this requirement in a developer. When it took 30 mins to scour the weighty tomes on the office shelf for an answer to a technical question, the ability to recall obscure facts and figures was essential. As developers in 2013 we all have access to all the information, all the time. The need for us to remember dusty corners of our chosen frameworks has gone. Get over it.

More important than CODE?!

It a well known phenomenon that the bar for developer tests is low. But why is this the case? Is it perhaps because a developer’s ability to write code is secondary? When hiring a developer, there are criteria that should be ranked higher than their ability to code. What could be more important than your code? Your ability to explain your code? Your ability to share your ideas with your colleagues? Are you excited by new developments in your chosen field? Your fit with the wider organisation culture? Your ability to plan your workload and prioritise? While a developers ability to write code is clearly a core skill, it is not the only one which should be considered when hiring. 

Take a sad song and make it better

I was recently asked to write a developer test as my organisation is looking to expand the current team and the existing test needed a little refresh. I wanted my developer test to not only test if the candidate could write code, but if the could read code and understand requirements. I also wanted my developer test to contain a variety of questions from simple programming ones – to make sure they understand general programming constructs like object-orientation – to tasks which our developers do everyday, like writing a LINQ query to filter a list of objects, or writing some JQuery to wire up click handlers. I included a few bug spotting questions to test candidates code reading abilities.

Finally, there was a section asking candidates to write a web page, showing a list of orders and the products contained within each order. A very low bar indeed, but this simple task tells me if the prospective candidate can structure their code, and write code which is reusable. Their ability to read and understand (albeit simple) instructions is also tested as well as their attention to detail.

Below are two sample questions to give a flavour of what I ended up with.

Define a Orange as a c# object. Include any object hierarchy and properties you see fit.

The following code contains some bugs. Please list below the code snippet any bugs you can see.

Hopefully my developer test will test more that just the candidates ability to retain information. Hopefully it will tell me more about the prospective candidates than other tests full of obscure framework related questions. Hopefully it will go some way to recruiting a better developer. Hopefully.

Entity Framework 5 and ValidateOnSaveEnabled

I have just started a new project and having had previous happy times with Entity Framework, I Nuget’ed in the latest version. All was going well until I tried to update an entity. I have the following code;

using (var c = new EFContext()) 
Data.User u = new Data.User() { UserId = UserId };
.FailedPasswordAttemptCount = newFailedPasswordAttemptCount;

In EF4, my understanding is that this would send an update statement to the database and only the FailedPasswordAttemptCount would be incremented for the user with the corresponding UserId.

In EF5, this craps out with a DbEntityValidationException as I have a non-nullable field in the user table (UserName).

I could solve this by creating my Data.User object with an empty UserName,

using (var c = new EFContext())
Data.User u = new Data.User() { UserId = UserId, UserName = "" };
.Entry(u).Property("FailedPasswordAttemptCount ").IsModified = true;

but this doesn’t seem particularly manageable as more non-nullable fields are added to this and numerous other tables.

It turns out there is now a property in EF called ValidateOnSaveEnabled ValidateOnSaveEnabled is a boolean which determines if entities should be validated automatically when SaveChanges() is invoked. It is set to true by default. Setting this to false got the code working again!

using (var c = new EFContext()) 
.Configuration.ValidateOnSaveEnabled = false;
Data.User u = new Data.User() { UserId = UserId };
.FailedPasswordAttemptCount = newFailedPasswordAttemptCount;