Web.API与集合一起发布复杂对象将集合发送为null

时间:2019-10-08 05:33:47

标签: c# xml serialization xmlhttprequest asp.net-web-api2

我正在努力运行一个接受xml的简单Web API服务。这是输入数据:

    <CertificateRequest>
       <CBRID>3</CBRID>
       <CBRBillingID>45</CBRBillingID>
       <CustomReferenceNo>24281</CustomReferenceNo>
       <RecipientId>NVWA</RecipientId>
       <DesiredIssueLocation>Cape Town</DesiredIssueLocation>
       <ConsignorId>D9999</ConsignorId>
       <ConsignorName>GROEP 91 UITVOER (EDMS) BPK</ConsignorName>
       <ConsignorAddressLine1>PLAAS LA GRATITUDE LT513</ConsignorAddressLine1>
       <ConsignorAddressLine2>GIYANI PAD (R529)</ConsignorAddressLine2>
       <ConsignorAddressLine3>LETSITELE, 0885</ConsignorAddressLine3>
       <ConsigneeId>GRO007</ConsigneeId>
       <ConsigneeName>GROEP 91 UITVOER (EDMS) BPK</ConsigneeName>
       <UnloadingBaseportLocation>NLRTM</UnloadingBaseportLocation>
       <TransportModeCode>1</TransportModeCode>
       <TransportMeans>Ocean Vessel: MSC ARBATAX</TransportMeans>
       <ConsignmentItems>
             <Description>GRAPEFRUIT  for Human Consumption</Description>
             <CommonName>GRAPEFRUIT</CommonName>
             <ScientificName>Citrus paradisi</ScientificName>
             <NetWeightMeasureCode>KGM</NetWeightMeasureCode>
....

要反序列化的类:

public class CertificateRequest
    {
        public CertificateRequest()
        {
            ConsignmentItems = new List<ConsignmentItem>();
            CertificateDocuments = new List<CertificateDocument>();
            CertificateFlexiFields = new List<CertificateFlexiField>();
            TradeUnits = new List<TradeUnitModel>();
        }
        /// <summary>
        /// The owner of the consignment
        /// </summary>
        [Required]
        public int CBRID { get; set; }
        /// <summary>
        /// The responsible person to bill
        /// </summary>
        [Required]
        public int CBRBillingID { get; set; }
        /// <summary>
        /// Sent by the consumer to reference their own system
        /// </summary>
        public string CustomReferenceNo { get; set; }
        /// <summary>
        /// The agreement to which the application is made for
        /// </summary>
        [Required]
        public string AgreementCode { get; set; }
        /// <summary>
        /// The Import Country’s NPPO
        /// </summary>
        public string RecipientId { get; set; } //Validate against valid list of recipients
        /// <summary>
        /// Location where Phyto will be issued
        /// </summary>
        [Required]
        public string DesiredIssueLocation { get; set; } //Stored List
        /// <summary>
        /// Exporter code
        /// </summary>
        public string ConsignorId { get; set; }
        /// <summary>
        /// Name of exporter
        /// </summary>
        [Required]
        public string ConsignorName { get; set; }
        /// <summary>
        /// Exporter address line 1
        /// </summary>
        [Required]
        public string ConsignorAddressLine1 { get; set; }
        /// <summary>
        /// Exporter address line 2
        /// </summary>
        public string ConsignorAddressLine2 { get; set; }
        /// <summary>
        /// Exporter address line 3
        /// </summary>
        public string ConsignorAddressLine3 { get; set; }
        /// <summary>
        /// The party importing the consignment’s business code 
        /// </summary>
        public string ConsigneeId { get; set; }
        /// <summary>
        /// Consignee name
        /// </summary>
        [Required]
        public string ConsigneeName { get; set; }
        /// <summary>
        /// Consignee address line 1
        /// </summary>
        [Required]
        public string ConsigneeAddressLine1 { get; set; }
        /// <summary>
        /// Consignee address line 2
        /// </summary>
        public string ConsigneeAddressLine2 { get; set; }
        /// <summary>
        /// Consignee address line 3
        /// </summary>
        public string ConsigneeAddressLine3 { get; set; }
        /// <summary>
        /// Consignee’s country (ISO Code 2)
        /// </summary>
        [Required]
        public string ConsigneeCountryId { get; set; } //ISO CODE - Validate against ISO list
        /// <summary>
        /// Importing Country (ISO Code 2)
        /// </summary>
        [Required]
        public string ImportCountryId { get; set; } //ISO CODE - Validate against ISO list
        /// <summary>
        /// The first point of arrival
        /// </summary>
        [Required]
        public string UnloadingBaseportLocation { get; set; } //ISO CODE - Validate against ISO list
        /// <summary>
        /// Type of transport used
        /// </summary>
        [Required]
        public string TransportModeCode { get; set; } //ISO CODE - Validate against ISO list
        /// <summary>
        /// The means of transport
        /// </summary>
        public string TransportMeans { get; set; } //ISO CODE - Validate against ISO list
        /// <summary>
        /// List of consignment items
        /// </summary>
        [XmlElement("ConsignmentItem")]
        public List<ConsignmentItem> ConsignmentItems { get; set; }

API控制器POST:

[HttpPost]
        [SwaggerResponse(HttpStatusCode.OK, Type = typeof(ApplyCertificateResponse), Description = "Successfully submitted application")]
        [SwaggerResponse(HttpStatusCode.BadRequest, Type = typeof(Response), Description = "Item invalid (code=400)")]
        public IHttpActionResult Apply([FromBody]CertificateRequest request)
        {
            if (request == null)
            {
                var badResponse = new Response
                {
                    IsSuccessful = false,
                    Message = "Invalid request object could not be deserialized, please check the object graph required!"
                };
                return Content(HttpStatusCode.BadRequest, badResponse);
            }
            //Validate application data
            var response = _certificateService.SaveCertificate(request);
            if (response.IsSuccessful)
            {
                var applicationResponse = new ApplyCertificateResponse
                {
                    Message = response.Message,
                    IsSuccessful = response.IsSuccessful,
                    CustomReferenceNo = response.Data.CustomReferenceNo,
                    ApplicationRefNo = response.Data.ApplicationReferenceNo,
                    ApplicationStatus = response.Data.ApplicationStatus,
                    DateCreated = response.Data.DateCreated
                };
                return Ok(applicationResponse);
            }
            return Ok(response);
        }

寄售托收总以某种方式为空。如果更改<ConsignmentItem>标签的名称,则传入的请求对象将完全为空。有没有人有示例项目或教程,我可以看一下以完成此简单任务。尝试了一切。

0 个答案:

没有答案