ASP.NET Core and MVC 6 Lessons Learned

I recently finished a small website using ASP.NET Core and MVC 6 - I only scratched the surface of the framework, but here's some gotchas and things I picked up along the way.

A Generic, IDisposable, Unit-Testable, Error-Handling WCF Service Client

I recently reused my generic, disposable WCF service client, and made some changes to make it simpler to use and easier to unit test. Here's what I did.

GetValueOrDefault() For An Expression of Arbitrary Length Using Expression Trees

In my most recent project we had to support classes with fairly deep object graphs where any of the nodes could validly be null. We didn't want to have lots of 'if not null' checks everywhere, so I wrote an extension method which takes an expression of any length and returns the expression value or a default value if any of the nodes are null. Here's the extension method code, along with an overview of how it works.

Extending WatiN 3: Detecting if a PDF Has Opened In a New Window

WatiN lets you attach to browser windows so you can verify their contents and close them as part of your tests, but the standard API doesn't detect windows with PDF documents in them. Here's an extension method which does.

Extending WatiN 2: Wait for JQuery document.ready() Functions to Complete

WatiN's DomContainer.WaitForComplete() method pauses test execution until the DOM has finished loading, but if your page has functions registered with JQuery's ready() function, you'll probably want to wait for those to finish executing before testing it. Here's a WatiN extension method which pauses test execution until that happens.

No More NCrunch For Me

NCrunch is a Visual Studio add-in which runs your tests while you work so you know if you've broken anything, as well as providing coverage indicators in the IDE and coverage metrics on demand. It recently went commercial, and time is running out for the free version I've been using for the last couple of months. From my experiences using NCrunch I'm going to let it expire, and go about my business without it. Here's why.

Unobtrusive Maximum Input Lengths with JQuery and FluentValidation

JQuery unobtrusive validation can be used to show an error message when the user inputs too many characters or a numeric value which is too big. On a recent project we wanted to use input's maxlength attribute to prevent a user from entering too many characters rather than cure the problem with an error message - here's how we did it.

Extending WatiN: Useful Extension Methods

I've been doing a fair amount of UI testing using WatiN recently – here’s some extension methods I've found useful.

A Generic, IDisposable WCF Service Client

WCF clients need to be cleaned up properly, but as they're usually auto-generated they don't implement IDisposable. I've been doing a fair bit of WCF work recently, so I wrote a generic WCF client wrapper which effectively gives me a disposable service client.

Fixing Chrome’s AJAX Request Caching Bug

I recently had to make a set of web pages restore their state when the user arrived on them after clicking the browser’s back button. I used JQuery's ajax function to get the data, but even with JQuery's cache: false setting and MVC's OutputCache attribute, Chrome insisted on retrieving it from its cache. Here's how I solved the problem.

Easier ASP.NET MVC Routing

I've recently refactored the way Routes are declared in an ASP.NET MVC application I'm working on, and I wanted to share part of the system I came up with; a really easy way to declare and keep track of ASP.NET MVC Routes, which then allows you to find the name of the Route which has been selected for the current request.

Handy Generic JQuery Functions

I was a bit of a late-comer to the JQuery party, but now I've been using it for a while it's given me a host of options for adding extra flair to the client side of my applications. Here's a few generic JQuery functions I've written which can be used to add some neat little features to a page. Just call any of them from a document ready function.

A Friendlier NUnit PredicateConstraint

I've taken to using NUnit's constraint model for my unit tests, as I find it a great way to write readable tests which state exactly what they prove. NUnit has lots of built-in Constraints, but its PredicateConstraint doesn't return very helpful error messages; here's a friendlier version, which does.

Populating ASP.NET MVC ViewModels using ViewModelBuilders

The ViewModels in my current project had got quite complex; as well as properties copied from model objects, they increasingly had flags used by Views to know whether to render links or sub-sections. The logic which set these properties was bloating Controllers, so I factored it out into objects which populate all non-editable properties of a ViewModel; ViewModelBuilders. Here's how :)

An Improved HttpModule to Redirect Requests When an Uploaded File is Too Big

Further to comments on my previous post on this topic, I realised my HttpModule which gives a user a friendly error message if they upload a file which is too big only dealt with half of the problem. The maximum size a request can be is specified in the machine.config and web.config in the maximumRequestLength attribute on the httpRuntime element; if a user uploads a file which is larger than that, the part of the HttpModule which checks the request size is by-passed, and the user gets a not-ver

Simple Solutions to Horrible Azure Pain - as Usual

I recently spent far too long debugging a problem with our Azure application. We have two Azure cloud projects, and at about the same time they both started refusing to start; the web applications they house both worked perfectly when deployed to local IIS. There turned out to be two different problems - both very simple - so I thought I'd share what they turned out to be :)

Using an HttpModule to Clean Up Form and QueryString Input

"A potentially dangerous Request.Form value was detected from the client". To avoid XSS attacks, ASP.NET throws a lovely yellow screen of death with that message. But sometimes you want to accept potentially dangerous input, or you just don't want to have that error splashed across the screen. Being a fan of prevention rather than cure, I figured I'd make an IHttpModule to screen input before it gets to my application.

Using an HttpModule to Redirect a Request When an Uploaded File is Too Big

Today I wanted to build something into an ASP.NET MVC application to impose a size limit on uploaded files. I wanted to use an HttpModule so I could get at the upload before any Controllers got involved, and redirect the request if the file was too big. I did this without much fuss, but the redirect went to an ASP.NET "Maximum request length exceeded" error page. I just wanted to do a redirect, darnit!

Using a custom IValueProvider for domain model objects in ASP.NET MVC 2

I had a series of ASP.NET MVC controllers which took identifiers for various domain objects, loaded the objects, then passed them to ViewModels. In order to DRY out the code a bit I decided to factor the object-loading logic into a customer IValueProvider - it turned out pretty neat, and this is how I did it :)