Skip to content

Instantly share code, notes, and snippets.

@codeprogression
Created February 28, 2012 12:14
Show Gist options
  • Save codeprogression/1932189 to your computer and use it in GitHub Desktop.
Save codeprogression/1932189 to your computer and use it in GitHub Desktop.
Adding alternative responses to the NancyContext.Items dictionary
public static void AddAlternateResponses<T>(this NancyContext context, T resource,
HttpStatusCode statusCode = HttpStatusCode.OK, string location = null)
{
var xmlResponse = new XmlResponse<T>(resource, "application/xml", new DefaultXmlSerializer())
{
StatusCode = statusCode
};
var jsonResponse = new JsonResponse(new { resource }, new DefaultJsonSerializer())
{
StatusCode = statusCode
};
if (location != null)
{
xmlResponse = (XmlResponse<T>) xmlResponse.WithHeader("Location", location);
jsonResponse = (JsonResponse) jsonResponse.WithHeader("Location", location);
}
context.Items.Add("Content-Negotiation", new List<Response> { xmlResponse, jsonResponse });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment