public HttpResponseMessage Count(string name, string collection, HttpRequestMessage request) { int count = ODataService.Count(name, collection, request.GetQueryNameValuePairs()); string mediaType = request.GetResponseMediaType(); switch (mediaType) { case "application/json": string json = string.Format("{{\"Count\": {0}}}", count); return(CreateHttpResponseMessage(json, request)); case "application/xml": XElement element = new XElement("Count", count); return(CreateHttpResponseMessage(element, request)); default: throw new NotSupportedException(mediaType.ToString()); } }
public HttpResponseMessage GetCollection(string name, string collection, HttpRequestMessage request) { int?count = null; if (IsPagingQuery(request)) { count = ODataService.Count(name, collection, request.GetQueryNameValuePairs()); } string mediaType = request.GetResponseMediaType(); switch (mediaType) { case "application/json": { ODataService <string> service = new ODataService <string>(name, request.GetQueryNameValuePairs()); IEnumerable <string> jsonCollection = service.GetCollection(collection); string json = string.Format("[{0}]", string.Join(",", jsonCollection)); if (count != null) { json = string.Format("{{\"@count\":{0},\"value\":{1}}}", count, json); } return(CreateHttpResponseMessage(json, request)); } case "application/xml": { ODataService <XElement> service = new ODataService <XElement>(name, request.GetQueryNameValuePairs()); IEnumerable <XElement> xCollection = service.GetCollection(collection, out XElement xsd); XElement element = new XElement(collection, xCollection); element.SetAttributeValue(XNamespace.Xmlns + "i", XSI); return(CreateHttpResponseMessage(Pack(element, count, xsd), request)); } default: throw new NotSupportedException(mediaType.ToString()); } }