6
votes

I am trying to get a Web API OData V4 endpoint up and running.

I got it going finally (after removing all the DateTime properties from my endpoint) and now the listing of the entities is in JSON.

I love JSON, but I use LinqPad to test my endpoints. It does not understand the JSON for the listing of the entities that are in my feed.

I have looked and can't seem to find a way to change this back to XML, so I am asking here.

Is there a way to have the listing of entities for a Web API OData v4 feed be in XML rather than JSON?

2
In the current V4 library $metadata is only supported in XML format, queries are only supported in JSON (Content-Type/Accept header or $format=.. query param is used to determine requested type). HTTP header name for OData version request/response has changed between V3/V4 (hence BreezeJs/DataJS not working). Cors support in Asp.Net/MVC/API/OData is scattered and doesn't work well together.Marvin Smit

2 Answers

14
votes

Sorry to post another answer, but my first one was getting too lengthy. I found this link: V4 always returns Json and sure enough, the very last suggestion does work:

In WebAPiConfig, add namespace references to:

using System.Net.Http.Formatting;
using System.Web.OData.Formatter;

and then add something like:

var formatters = ODataMediaTypeFormatters.Create();
config.Formatters.InsertRange(0, formatters);

The entity listing now gets returned as xml.

The downside, is now all the responses default to the less preferred, verbose xml/atom.

The upside, is that the $format request is now honored in v4. So to get back to json, you can issue the url (without messing with headers) as: http://<myodataurl>?$format=application/json;odata.metadata=full (or minimal or none)

However, as stated previous, LinqPad still does not recognize the v4 schema, and will not connect correctly to this endpoint.

2
votes

If you have setup a Web API service using the basic, default configurations that the MS walkthroughs suggest, then the response formatter is already configured and will use json by default, or xml if so prompted.

So the prompt for an xml response usually comes from the client request. At its basic form, the request will contain a Accept: application/atom+xml,application/xml header. If it does not, I believe the Web API response will default to json.

For your particular question, there is an alternative for LinqPad. When you setup your OData connection, the dialog has a Formatter option of Xml or Json, which tells Linqpad to issue the corresponding Accept header in its request.

Using Fiddler to monitor the traffic, if you set the LinqPad OData connection to use the xml formatter, the request contains the headers:

MaxDataServiceVersion: 3.0;NetFx
Accept: application/atom+xml,application/xml

and the response comes back as an atom/xml feed.

If you set the option to use the json formatter, the request contains the headers:

MaxDataServiceVersion: 3.0;NetFx
Accept: application/json;odata=minimalmetadata

and the response comes back as json.

For oData v3, LinqPad handles both just fine.

Edit

This now sounds vaguely familiar with some test I ran a short time ago...I'm not sure if LinqPad supports the v4 protocol yet. It uses WCF Data Services client under the covers, and those libraries stopped at OData v3. And, in fact, as you see from the headers above, the request will only talk to a v3 service.

So your issue is not that the Web Api is not supporting both xml and json. The issue is that LinqPad does not yet connect to a v4 oData service. Using Fiddler or other similar tool, you should be able to request both json and/or xml from the service.