While writing tests for an application, one test I created failed unexpectedly.
The code it tested was the following:
This code reads all querystring parameters from the current request excluding a set of predefined parameters. I use this code to capture all query string parameters that are not explicitely bound to an action method parameter:
Do you spot the bug? The problem was that this code only worked when the query string parameter name has the same casing as the parameter names in my code. If the casing was different not all parameters where excluded.
To explain this further, following querystring worked as expected:
?foldername=root&isProtected=false&tag1=value1
Whereas the following querystring didn’t do the trick:
?folderName=root&IsProtected=false&tag1=value1
To fix it, I had to update the Contains() check to be case-insensitive. This can be done by using an overload that allows you to specify an IEqualityComparer<T> implementation.
First I was thinking I had to implement this interface myself, but luckily a built-in solution exists through the usage of the StringComparer class:
The StringComparer.OrdinalIgnoreCase implementation was exactly what I needed…