未捕获MVC Web服务XML字段

时间:2018-09-12 16:20:12

标签: asp.net-web-api

我已经创建了一个MVC Web服务。该服务接受带有表示票证的XML文件的httppost。 并非所有XML字段都传输到票证对象实例。 但是,当我更改XML文件中节点的顺序时,将捕获其他字段。 但是我找不到任何逻辑。

TicketController:

using System;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace Webservice.Controllers
{
    public class TicketController : ApiController
    {
        [HttpPost]
        public async Task<string> Add(Ticket ticket)
        {
            return await Handler.CreateTicketAsync(ticket);
        }
    }
}

“票务”类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
namespace Eurosys.Freshdesk.Webservice.Models
{
    [DataContract]
    public class Ticket
    {
        [DataMember(Name = "id")]
        public string Id { get; set; }

        [DataMember(Name = "companyid")]
        public string CompanyId { get; set; }

        [DataMember(Name = "email")]
        public string Email { get; set; }

        [DataMember(Name = "contactname")]
        public string ContactName { get; set; }

        [DataMember(Name = "contactphone")]
        public string ContactPhone { get; set; }

        [DataMember(Name = "contactmobile")]
        public string ContactMobile { get; set; }

        [DataMember(Name = "priority")]
        public int Priority { get; set; }

        [DataMember(Name = "subject")]
        public string Subject { get; set; }

        [DataMember(Name = "long_description")]
        public string LongDescription { get; set; }
    }
}

我的XML文件如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Ticket xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Webservice.Models">
  <id>1</id>
  <companyid>2</companyid>  
  <email>nomail@gmail.com</email>
  <contactname>John Doe</contactname>   
  <contactphone>3</contactphone>
  <priority>1</priority>
  <subject>testsubject</subject>   
  <long_description>testdescription</long_description>
</Ticket>

在票证对象中更新字段ID,优先级和主题。所有其他字段为空

当我更改XML文件中的顺序时,例如(因此,在ID之前输入电子邮件):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Ticket xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Webservice.Models">
  <email>nomail@gmail.com</email>
  <id>1</id>
  <companyid>2</companyid>  
  <contactname>John Doe</contactname>   
  <contactphone>3</contactphone>
  <priority>1</priority>
  <subject>testsubject</subject>   
  <long_description>testdescription</long_description>
</Ticket>

Ticket.Id,Priority和LongDescription被填充,Ticket.Email也被填充。

当我更改XML文件中的顺序时,例如(所以ID之前是主题):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Ticket xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Webservice.Models">
  <email>nomail@gmail.com</email>
  <subject>testsubject</subject>   
  <id>1</id>
  <companyid>2</companyid>  
  <contactname>John Doe</contactname>   
  <contactphone>3</contactphone>
  <priority>1</priority>
  <long_description>testdescription</long_description>
</Ticket>

现在:ticket.Email和ticket.Subject已填充,但是ticket.Longdescription和ticket.Priority不再存在

逻辑是什么?为什么没有填写所有票务字段?

1 个答案:

答案 0 :(得分:0)

XML请求中的字段必须按字母顺序。