Friday, July 1, 2016

The route parameter name " is invalid. Route parameter names must be non-empty and cannot contain these characters

I was recently working on debugging an ASP.NET Web API web application, when I was suddenly faced with this error message:



Of course, as soon as developers are involved and something breaks, developers start playing the "Blame Game"!  So I was immediately blamed for the exception based on my changes to WebApiConfig.cs.

So, in any case, I decided to do some investigation to see what was the actual root cause of the problem.  When I did a revert to my commit in source control, I discovered my changes continued to work just fine!

Therefore, it had do be a problem with a commit made by another developer.  Once I started going through the history of check-ins in source control, I came across this line:

[Route("{id}/{}")]
 
Well, as soon as I discovered this I immediately understood the problem!  We were using Attribute routing in our application and the developer had mistakenly forgotten to enter the last parameter in the curly braces!

I don't think anyone realized (including myself or the developer), that this one invalid route in the code base would completely affect the entire ASP.NET Web API Web Application, but surprisingly it did!

Therefore, one of the cautions to learn from this while using Attribute-based Routing is that a typo in the code can quickly break the entire application and this type of change can be very, very difficult to discover!  This is yet another important reason to have Unit Tests and Continuous Integration build and deployment processes in place so that these types of defects can be corrected quickly!!

No comments:

Post a Comment