Reputation: 40575
In the hopes of supplying more metadata to my application, I wanted to wrap some extra information around my database queries (using plain on SQL data access, no Entity Framework or other ORMs).
I have an APIQueryResult
object set up as:
string Status { get; set; }
string[] Errors { get; set; }
object[] Data { get; set;}
The Data
is an array of my POCO object(s).
If I request JSON, then everything serializes just fine, including my metadata + object data. However, if I request XML, then I get the following error:
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
Note: This idea was inspired by Filip W (http://www.strathweb.com/2012/06/extending-your-asp-net-web-api-responses-with-useful-metadata/ ) but I didn't want to get as complicated as writing HttpHandlers, etc.
Is there a better way to provide at least error information back to the client if I can't return a normal class, or IEnumerable/List of class instances?
Thanks!
Upvotes: 0
Views: 592
Reputation: 142222
You could implement IXmlSerializable on your APIQueryResult object. With a bit of reflection it shouldn't be more than 20-30 lines with an XmlWriter to manually serialize that information. I guess it depends on how complex your DTOs are. The advantage of that is you have complete control over how what your XML looks like.
Upvotes: 1