WebAPI OData $ format for xml

for my WebAPI OData application, I am trying to give my client (browser) a decision in what format the data output should be. since $ format is not yet implemented in WebAPI OData, im using the Raghuramn example: https://gist.github.com/raghuramn/5556691

var queryParams = request.GetQueryNameValuePairs(); var dollarFormat = queryParams.Where(kvp => kvp.Key == "$format").Select(kvp => kvp.Value).FirstOrDefault(); if (dollarFormat != null) { request.Headers.Accept.Clear(); request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(dollarFormat)); // remove $format from the request. request.Properties[HttpPropertyKeys.RequestQueryNameValuePairsKey] = queryParams.Where(kvp => kvp.Key != "$format"); } 

This works for JSON ($ format = application / json; odata = fullmetadata) and JSON light (format = application / json; odata = light), but not for xml yet.

if I add $ format = application / XML to the query string, it still produces json. how to force xml output?

EDIT:

even if I force xml to Fiddler by sending Content-type: application / xml and Accept: application / xml with the request, the answer simply lists: Content-Type: application / json; OData = minimalmetadata; streaming = TRUE; encoding = UTF-8

EDIT 2:

Accept: application / atom + xml seems to output xml in the original answer. Unfortunately, "application / atom + xml" throws a FormatException in:

 request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/atom+xml")); 
+6
source share
2 answers

setting the ContentType request instead of AcceptHeader did the trick:

 request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/atom+xml"); 
+3
source

Thanks to the keyword search request.Headers.Accept.Add and MediaTypeWithQualityHeaderValue that were presented by this question, I found a CodeProject article that actually provided the syntax to correctly add the Accept header and solve the same problem:

 this.Request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/atom+xml")); 

instead of MediaTypeWithQualityHeaderValue.Parse("application/atom+xml") , which throws a FormatException.

0
source

All Articles