I have an object that has two levels of nested properties, as illustrated below. This is the *exact* string emitted by my web api endpoint for a GET request. When I POST this, it enters the handler correctly, but inspection of the resulting object (using
the same model that the GET used to generate the string) shows that the items collection is null. I've tried this as both XML and JSON.
Is there anything obvious that I might be missing?
Sure, the three relevant type are listed below. I've not done anything to specifically configure any type formatters. I forgot to mention that the LJOrder object is created, and the OrderID, StoreID and Name properties are correctly populated, it's just
the Items list that is null.
public class LJOrder
{
public int OrderID { get; set; }
public int StoreID { get; set; }
public string Name { get; set; }
public List<OrderItem> Items { get; set; }
}
public class OrderItem
{
public int OrderItemID { get; set; }
public int ProductID { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public List<OrderItemOption> ItemOptions { get; set; }
}
public class OrderItemOption
{
public int ProductOptionID { get; set; }
public decimal Price { get; set; }
}
thicker2k12
0 Points
6 Posts
POST can't read nested object
Jun 05, 2012 10:27 PM|LINK
I have an object that has two levels of nested properties, as illustrated below. This is the *exact* string emitted by my web api endpoint for a GET request. When I POST this, it enters the handler correctly, but inspection of the resulting object (using the same model that the GET used to generate the string) shows that the items collection is null. I've tried this as both XML and JSON.
Is there anything obvious that I might be missing?
<LJOrder>
<OrderID>7</OrderID>
<StoreID>8</StoreID>
<Name>Eric</Name>
<Items>
<OrderItem>
<OrderItemID>7</OrderItemID>
<ProductID>23</ProductID>
<Quantity>1</Quantity>
<UnitPrice>0.0000</UnitPrice>
<ItemOptions>
<OrderItemOption>
<ProductOptionID>6</ProductOptionID>
<Price>6.2900</Price>
</OrderItemOption>
</ItemOptions>
</OrderItem>
</Items>
</LJOrder>
Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: POST can't read nested object
Jun 06, 2012 12:30 AM|LINK
Are you using Beta or RC bits?
Kiran Challa
mstrother
Member
42 Points
10 Posts
Re: POST can't read nested object
Jun 06, 2012 12:32 AM|LINK
The release candidate.
dravva
Member
142 Points
31 Posts
Microsoft
Re: POST can't read nested object
Jun 06, 2012 06:19 AM|LINK
Can you share us the LJOrder type info? Are the xml/json mediatypeformatter(s) default ones or are they configured specifically?
thicker2k12
0 Points
6 Posts
Re: POST can't read nested object
Jun 06, 2012 03:15 PM|LINK
Sure, the three relevant type are listed below. I've not done anything to specifically configure any type formatters. I forgot to mention that the LJOrder object is created, and the OrderID, StoreID and Name properties are correctly populated, it's just the Items list that is null.
public class LJOrder
{
public int OrderID { get; set; }
public int StoreID { get; set; }
public string Name { get; set; }
public List<OrderItem> Items { get; set; }
}
public class OrderItem
{
public int OrderItemID { get; set; }
public int ProductID { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public List<OrderItemOption> ItemOptions { get; set; }
}
public class OrderItemOption
{
public int ProductOptionID { get; set; }
public decimal Price { get; set; }
}
thicker2k12
0 Points
6 Posts
Re: POST can't read nested object
Jun 06, 2012 06:54 PM|LINK
I was using the beta, updated to RC and now the whole order object is null.
public HttpResponseMessage Post(LJOrder order)
{
this.repository.Post(order);
var response = Request.CreateResponse(HttpStatusCode.Created, order);
return response;
}
Yao Huang Li...
Member
38 Points
9 Posts
Microsoft
Re: POST can't read nested object
Jun 07, 2012 06:48 AM|LINK
Maybe this is because the default serializer used in XmlMediaTypeFormatter has been changed from XmlSerializer to DataContractSerializer?
Can you try the following HttpConfiguration setting?
Hope this helps,
Yao
Pierre Lebel
Member
9 Points
28 Posts
Re: POST can't read nested object
Jun 07, 2012 03:27 PM|LINK
I encountered a similar problem yesterday while testing MVC4 on RC
I figured that if I was posting the same XML format as I received from GET, it worked.
I realise that the XML required a name space which is the name space where my model was defined
xmlns="http://schemas.datacontract.org/2004/07/MvcApplication8.Models
namespace MvcApplication8.Models
{
public class Sale
{
public string name;
public string item;
public string price;
}
}
So my post body which should look like this
<Sale><item>Carrots</item><name>Emilye Healthy</name><price>0.27</price></Sale>
has to look like this
<Sale xmlns="http://schemas.datacontract.org/2004/07/MvcApplication8.Models"><item>Carrots</item><name>Emilye Healthy</name><price>0.27</price></Sale>
It doesn't make sense to me that's why I will try to use the good old XML Serializer
Pierre Lebel
Member
9 Points
28 Posts
Re: POST can't read nested object
Jun 07, 2012 03:41 PM|LINK
The recommendation posted by Yao Huang Lin works for me, I no longer need to specify the namespace in the XML.
Great. I won't have to overwrite the XML formatter anymore.
By default, this is turned off.
Here is how I did it in my controller.
using System.Web.Http.Controllers;
public class ValuesController : ApiController
{
protected override void Initialize(HttpControllerContext controllerContext)
{
controllerContext.Configuration.Formatters.XmlFormatter.UseXmlSerializer =true;
}
...
thicker2k12
0 Points
6 Posts
Re: POST can't read nested object
Jun 07, 2012 08:39 PM|LINK
Tried this, now I'm getting:
"The configured formatter 'System.Net.Http.Formatting.XmlMediaTypeFormatter' cannot write an object of type 'EnumerableQuery`1'."
Note that my GETs are declared as IQueryable, I got this from the Contacts sample.