2013-01-15 71 views
1

我正在使用.Net的Web API編寫一個簡單的API。我已經定義了以下型號:.Net Web API SerializationException

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/2 004/07/API.Models'..遇到 '元素'名爲'VehicleUpdate',命名空間''。在 System.Runtime.Serialization.DataContractSerializer.InternalReadObject處 系統 System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator 讀卡器,布爾verifyObjectName,DataContractResolver dataContractResolver)(XmlReaderDelegator 的XmlReader,布爾verifyObjectName,DataContractResolver dataContractResolver)。 Runtime.Serialization.DataContractSerializer.ReadObject(XmlReader reader)at System.Net.Http.Formatting.XmlMediaTypeFormatter。 <> C_ DisplayClass3.b _2()

有人可以告訴我什麼,我做錯了什麼?我認爲這將是一個有效的XML消息。

回答

5

兩件事情:

AS錯誤說,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。

當您在非空值類型int上使用[Required]時,您可能會遇到另一個問題。這是一個已知的問題,因爲如果不通過,int將始終爲0而不是null。在這種情況下,你可能需要改變你的模型有excplicit 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; } 
    } 
+0

謝謝!我添加了一個引用和使用語句System.Runtime.Serialization – jkruer01

+0

然後我用DataContract和DataMember聲明更新了我的模型,它工作。謝謝! – jkruer01

0
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; 
}