You have probably already heard about Nancy, the web framework created by Elegant Code’s own Andreas Håkansson. Andreas and Steven Robbins, custodians of the Super-Duper-Happy-Path, have coordinated the evolution of the framework that I have come to very-much appreciate.
My coworkers and I have been working on a new API for our products, and Nancy has been our framework of choice. One thing we know we will need for the consumers of the API is content-negotiation. Currently, Nancy does not have content-negotiation built in to the framework; although, it is planned for a future release.
Content-Negotiation is the process in which a User-Agent and Server determine which, if any, of a weighted list of preferred representations is returned in response to a URL request. The representations are often based on MIME type, but can be based on language or encoding. In the case of our API, we are concerned with MIME type; specifically, JSON vs. XML.
Even though Nancy does not have content-negotiation yet, the Nancy team has been prepping for the feature by adding support for weighted Accept headers. This makes it fairly trivial to add content-negotiation on a per-route basis.
After some experimentation (including creating a pipeline handler, though it was admittedly ugly), plus a couple of conversations in the NancyFx Jabbr room, I came up with the following extension method: