简体   繁体   中英

How to post complex object with a child collection in WebAPI

I am trying to post an object in my controller but it is coming in null. The object is a complex type and suspect it is the reason why Im getting an exception.

public class CertificateRequest
{
     public string Name {get;set;}
     public string RequestNumber {get;set;}

     public List<TradeUnit> TradeUnits {get;set;}
}
public class TradeUnit
{
     public string TradeUnitNumber {get;set;}
}

Controller:

[HttpPost]
public IHttpActionResult Post(CertificateRequest req)
{
     ........
}

When I check Swagger, it appears the object its expecting is silently different.

<CertificateRequest>
   <Name>Some Name</Name>
   <RequestNumber>Req001</RequestNumber>
   <TradeUnits>
       <TradeUnitNumber>TUN0005</TradeUnitNumber>
   </TradeUnits>
</CertificateRequest>

The problem happens when I have multiple TradeUnits I would like to post and would have my structure to be in the following format, how do I achieve this.

<CertificateRequest>
   <Name>Some Name</Name>
   <RequestNumber>Req001</RequestNumber>
   <TradeUnits>
       <TradeUnit>
            <TradeUnitNumber>TUN0001</TradeUnitNumber>
       </TradeUnit>
       <TradeUnit>
            <TradeUnitNumber>TUN0002</TradeUnitNumber>
       </TradeUnit>       
       <TradeUnit>
            <TradeUnitNumber>TUN0003</TradeUnitNumber>
       </TradeUnit>
   </TradeUnits>
</CertificateRequest>

How can I achieve this?

Try following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication135
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            XmlWriter writer = XmlWriter.Create(FILENAME, settings);

            CertificateRequest request = new CertificateRequest();
            XmlSerializer serializer = new XmlSerializer(typeof(CertificateRequest));
            serializer.Serialize(writer, request);
        }
    }
    public class CertificateRequest
    {
        public string Name { get; set; }
        public string RequestNumber { get; set; }

        [XmlArray("TradeUnits")]
        [XmlArrayItem("TradeUnit")]
        public List<TradeUnit> tradeUnits { get; set; }

        public CertificateRequest()
        {
            Name = "Some Name";
            RequestNumber = "Req001";

            tradeUnits = new List<TradeUnit>() {
                new TradeUnit() { TradeUnitNumber = "TUN0001"},
                new TradeUnit() { TradeUnitNumber = "TUN0002"},
                new TradeUnit() { TradeUnitNumber = "TUN0003"}
            };
        }
    }
    public class TradeUnit
    {
        public string TradeUnitNumber { get; set; }
    }

}

Using following will eliminate a tag

        [XmlElement("TradeUnit")]
        public List<TradeUnit> tradeUnits { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM