2
votes

How can you produce ATOM formatted output in an ASP.NET WebApi 2.2 ODATA service? Creating the JSON version, or the simple XML format is easy. But no matter how I request the Content-Type, I always get the 1st format in the configuration. (Using PostMan for Chrome, or setting the Request's Content-Type in the producer method.)

If I use WCF Data Service, I get ATOM formatted result. But as far as I understand, the ODATA v4 is only implemented in WebApi, not in WCF. So, it seems a bit odd, that I can't format it any way I like...

My configuration code is the basic:

   config.MapODataServiceRoute(
        routeName: "ODataRoute",
        routePrefix: null,
        model: builder.GetEdmModel());

Thanks,

AntiTalent

UPDATE: Using the typical solution found on the net (link from 1st comment, @mdisibrio), I get this (WebApi 2.2):

<ODataServiceDocument 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.datacontract.org/2004/07/Microsoft.OData.Core">
  <EntitySets>
    <ODataEntitySetInfo>
      <Name>Projects</Name>
      <Title i:nil="true"/>
      <Url>Projects</Url>
    </ODataEntitySetInfo>
  </EntitySets>
  <FunctionImports/>
  <Singletons/>
</ODataServiceDocument>

But what I would like to get is (WCF Data Service):

<service xmlns="http://www.w3.org/2007/app"
 xmlns:atom="http://www.w3.org/2005/Atom" 
 xml:base="http://MYSERVER/Service.svc/">
  <workspace>
    <atom:title>Default</atom:title>
    <collection href="ProjectList">
    <atom:title>ProjectList</atom:title>
    </collection>
  </workspace>
</service>

Yes, I'm fully aware, that the entities have different names. It's not what my issue is.

1
Does this answer address what you are trying to accomplish? It will add an atom formatter to the request handlers. Unfortunately, v4 does not add it by default.mdisibio
@mdisibio: Thanks. Already tried it. It does produce XML, but not in the correct format. See my updates.AntiTalent
Arrrg...It took me so long to figure out how to get an xml response, that I never noticed it was plain-old xml and not Atom. This is the closest explanation I've found that makes any sense, as of the the 6.1 libs: OData core libraries are capable of serializing the OData v4 Atom format but this is not officially supported since Atom specification is not at CS2 stage yet.mdisibio

1 Answers

2
votes

Borrowing from this link: How to disable Formatters in Web Api OData web service

You can add whatever formatters you wish to use in your WebApiConfig. So for your example, I think you want to do this:

var odataFormatters = ODataMediaTypeFormatters.Create();
odataFormatters = odataFormatters.Where(
    f => f.SupportedMediaTypes.Any(
        m => m.MediaType == "application/atom+xml" ||
            m.MediaType == "application/atomsvc+xml")).ToList();

config.Formatters.Clear();
config.Formatters.AddRange(odataFormatters);