2

Closed

$format is not supported

description

ODATA services should support $format ($format=json and $format=atom) query string parameters.
As described here http://www.odata.org/documentation/uri-conventions#FormatSystemQueryOption .
Closed Mar 30 at 12:02 AM by HongmeiG
dup of 278

comments

gforce0803 wrote Oct 26, 2012 at 7:37 PM

I remember reading somewhere that this was dropped in favour of the having the consumer provide the Accept: application/json header; However I think this should be supported and handled by an media formatter which looks at the request and injects the correct Accept: <value> header if the $format=<value> was supplied and/or defaults to the json format.

just my two pennies worth ;-)

Grahame

raghuramn wrote Oct 26, 2012 at 8:38 PM

Grahame,

It is simple to do this yourself with QueryStringMapping. All you need to do is

formatter.MediaTypeMappings.Add(new QueryStringMapping("$format", "json", "application/json;odata=verbose");

formatter.MediaTypeMappings.Add(new QueryStringMapping("$format", "xml", "application/atom+xml");

gforce0803 wrote Oct 29, 2012 at 10:51 AM

raghuramn,

I would concider the MediaTypeMapping as a work-around; I think that this should be applied as default (out-of-the-box) on odata/queryable ApiController methods given it's part of the odata specification.

I'm thinking at present that it may need hooks into the Action Routing/Selection to determine if the URI points to a method that is marked as with Queryable and returning IQueryable<T>; if so has the URI request got a $format= parameter

cheers
Grahame

drdamour wrote Mar 19 at 8:49 PM

i believe this is a duplicate of http://aspnetwebstack.codeplex.com/workitem/278 should be merged

vgolynets wrote Tue at 2:43 PM

Actually solution proposed by raghuramn does not work.
I tried these variants:
config.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/atom+xml");

config.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("$format", "xml", "application/atom+xml"));

And both of them do not work.

The only way that works for me I found in this post http://stackoverflow.com/questions/16455501/why-doesnt-my-odata-response-have-navigation-properties

Post above has another reference to https://gist.github.com/raghuramn/5556691 by RaghuRam Nadiminti.

RaghuRam Nadiminti advised to add custom handler into config.MessageHandlers that could override Accept header.

So question, is advise about formatters actual?