Wednesday 18 September 2013

Obscure ASP.Net MVC4 WebAPI XML POST parameter

So I had this problem with getting simple XML to be recognized by a Web API controller.

The type that has no namespaces, the type that people cook up in a couple of minutes in Javascript then try to post to a RESTful API.

I started with switching to the XmlSerializer... http://stackoverflow.com/a/11804373/573261. Result is as follows:
System.InvalidOperationException: No MediaTypeFormatter is available to read an object of type 'Job' from content with media type 'application/xml'
I also tried changing the XML (body content) on the fly... http://stackoverflow.com/q/14365953/573261

In between those two attempts were others that you would rather not want to know about, since they obviously led nowhere.

In the end, it turns out that the XML Serializer cannot handle interfaced properties.

        public IList<Item> Items { get; set; }    // fails
        public List<Item> Items { get; set; }    // works

Now you know.