Created
February 28, 2012 12:14
-
-
Save codeprogression/1932189 to your computer and use it in GitHub Desktop.
Adding alternative responses to the NancyContext.Items dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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