.Net Web API SerializationException

时间:2013-01-15 20:31:43

标签: asp.net .net xml-serialization asp.net-web-api wcf-web-api

我正在使用.Net的Web API编写一个简单的API。我定义了以下模型:

public class VehicleUpdate
{
    [Required(ErrorMessage = "DealerID Required")]
    public int DealerID { get; set; }

    [Required(ErrorMessage = "VIN Required")]
    [StringLength(17, ErrorMessage = "VIN Must be 17 characters", MinimumLength = 17)]
    public string VIN { get; set; }

    [StringLength(8000, ErrorMessage = "Comments must be less than 8,000 characters")]
    public string Comments { get; set; }

    public double Retail { get; set; }
}

我尝试通过使用以下XML

执行HTTP Post来测试它
<VehicleUpdate>
    <DealerID>30</DealerID>
    <VIN>1FMRU17L0WLA62356</VIN>
    <Comments>This is a test.</Comments>
    <Retail>1000</Retail>
</VehicleUpdate>

当我这样做时,我得到以下SerializationException:

  

System.Runtime.Serialization.SerializationException:第1行出错   位置16.期望来自命名空间的元素'VehicleUpdate'   'http://schemas.datacontract.org/2004/07/API.Models'......遇到了   'Element',名称为'VehicleUpdate',名称空间''。在   System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator   xmlReader,Boolean verifyObjectName,DataContractResolver   dataContractResolver)at   System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator   reader,Boolean verifyObjectName,DataContractResolver   dataContractResolver)at   System.Runtime.Serialization.DataContractSerializer.ReadObject(的XmlReader   读者)   System.Net.Http.Formatting.XmlMediaTypeFormatter&LT;&GT; C_ DisplayClass3.b _2()

有人可以告诉我我做错了什么吗?我认为这将是一个有效的XML消息。

2 个答案:

答案 0 :(得分:6)

两件事:

如错误所述,DataContractSerializer期望XML提供适当的命名空间,而您的XML没有。 您可以传递名称空间:

<VehicleUpdate xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/API.Models">
 // properties
</VehicleUpdate>

或者在Web API配置中切换到XmlSerializer

config.Formatters.XmlFormatter.UseXmlSerializer = true;

然后你可以传递你现在传递的确切XML。

在非可空类型[Required]上使用int时,您可能会遇到另一个问题。这是一个已知问题,因为如果未传递,int将始终为0而不是null。在这种情况下,您可能需要更改模型以使用明确的DataContract定义:

  [DataContract]
    public class VehicleUpdate
    {
        [DataMember(IsRequired = true)]
        [Required(ErrorMessage = "DealerID Required")]
        public int DealerID { get; set; }

        [DataMember]
        [Required(ErrorMessage = "VIN Required")]
        [StringLength(17, ErrorMessage = "VIN Must be 17 characters", MinimumLength = 17)]
        public string VIN { get; set; }

        [DataMember]
        [StringLength(8000, ErrorMessage = "Comments must be less than 8,000 characters")]
        public string Comments { get; set; }

        [DataMember]
        public double Retail { get; set; }
    }

答案 1 :(得分:1)

public static class WebApiConfig
    {
//....
        public static void Register(HttpConfiguration config)
        {  config.Formatters.XmlFormatter.UseXmlSerializer = true;
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling =
        Newtonsoft.Json.PreserveReferencesHandling.Objects;
}