This project is read-only.

Web API OData - atom+xml formatting issue when getting individual properties

Topics: ASP.NET Web API
May 16, 2013 at 1:47 PM

I am using Web API OData library of version 4.0.0 and having an issue with atom+xml formatting when accessing individual properties of my model.

I created a very basic example to demonstrate the issue.
The model classes are very simple: we have a Person entity which has a collection of addresses. The address is not a separate entity - it is just a part of Person.
public class Person
        public int Id { get; set; }
        public string Name { get; set; }
        public IEnumerable<Address> Addresses { get; set; }

    public class Address
        public string City { get; set; }
        public string Street { get; set; }
The Edm model is configured using ODataConventionalModelBuilder:
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
The controller for Persons resource is below.
It allows to get a Person by Id ( http://service/Persons(1) ) and to get addresses collection of a single Person ( http://service/Persons(1)/Addresses )
public class PersonsController : ODataController

        public Person Get(int key)
            return _list.FirstOrDefault(p => p.Id == key);

        public IEnumerable<Address> GetAddresses(int key)
            return _list.FirstOrDefault(p => p.Id == key).Addresses;
In order to get addresses I've implemented a custom routing convention to support the OData path ~/entityset/key/property because it is not supported by default (this was discussed here).

When I access URL like /service/Persons(1), it works perfectly with both application/json and application/atom+xml Accept header.

But when I try to access /service/Persons(1)/Addresses and pass application/atom+xml header - it doesn't work, the response is returned in JSON format.
However, if I specify application/xml header (without atom), the response format is a plain XML.

I expect that application/atom+xml should work for both situations that I described.

Is this a bug or am I doing something wrong?

Thank you,
May 20, 2013 at 12:08 AM
Complex properties are formatted as application/xml. Only entries and feeds (entities and collection of entities) are formatted as application/atom+xml. So, the behavior that you are seeing is correct.

You can get the behavior that you want by putting application/atom+xml and application/xml in the accept header like, "application/atom+xml,application/xml"
May 20, 2013 at 7:58 AM
Thank you!

That's what I wanted to know.