Search code examples
.netodataasp.net-web-api-odataodata-v4

Get the OData catalog for Web API OData v4 in XML


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?


Solution

  • 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.