public static Response CreateResponse(this Exception e, ResponseFormat format, IResponseFormatter formatter) { // dumb ugliness b/c MSFT's xml serializer can't handle anonymous objects var exception = new FormattedException { name = e.GetType().Name, message = e.Message, callStack = e.StackTrace }; Response r; switch (format) { case ResponseFormat.Json: r = formatter.AsJson(exception, HttpStatusCode.InternalServerError); break; case ResponseFormat.Xml: r = formatter.AsXml(exception); break; default: r = null; break; } if (r != null) { r.StatusCode = HttpStatusCode.InternalServerError; } return r; }
public void Should_return_a_null_in_xml_format() { using (var stream = new MemoryStream()) { formatter.AsXml <Person>(null).Contents(stream); var root = GetXmlRoot(stream); root.GetAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance").ShouldEqual("true"); root.ChildNodes.Count.ShouldEqual(0); } }
public static Response WithContent <T>(this IResponseFormatter formatter, IEnumerable <Tuple <string, decimal> > acceptHeaders, T content) { var xmlWeight = CalculateWeightForContentType(acceptHeaders, "xml"); var jsonWeight = CalculateWeightForContentType(acceptHeaders, "json"); if (jsonWeight > xmlWeight) { return(formatter.AsJson(content)); } else { return(formatter.AsXml(content)); } }