2014-11-13 106 views
0

我有以下Web API操作。Web Api - 'ObjectContent類型未能序列化

public IHttpActionResult Get() { 
    var units = Enum.GetValues(typeof(UnitType)).Cast<UnitType>().Select(x => new { Id = (Int32)x, Description = x.Attribute<DescriptionAttribute>().Description }); 
    return Ok(units); 
} 

基本上,我返回一個枚舉的值和說明。

我檢查了單位列表,我得到了正確的值。

然而,當我打電話,我得到以下錯誤的API:

<Error><Message>An error has occurred.</Message> 
<ExceptionMessage>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.</ExceptionMessage> 
<ExceptionType>System.InvalidOperationException</ExceptionType> 
<StackTrace/><InnerException><Message>An error has occurred.</Message> 
<ExceptionMessage>Type '<>f__AnonymousType0`2[System.Int32,System.String]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.</ExceptionMessage> 

爲什麼?

UPDATE

我現在有:

public IHttpActionResult Get() { 
    IList<EnumModel> units = Enum.GetValues(typeof(UnitType)).Cast<UnitType>().Select(x => new EnumModel((Int32)x, x.Attribute<DescriptionAttribute>().Description)).ToList(); 
    return Ok(units); 
    } // Get 

public class EnumModel { 
    public Int32 Id { get; set; } 
    public String Description { get; set; } 

    public EnumModel(Int32 id, String description) { 
    Id = id; 
    Description = description; 
    } // EnumModel 
} // EnumModel 

我得到的錯誤:

<Error> 
    <Message>An error has occurred.</Message> 
    <ExceptionMessage>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.</ExceptionMessage> 
    <ExceptionType>System.InvalidOperationException</ExceptionType> 
    <StackTrace/><InnerException> 
    <Message>An error has occurred.</Message> 
    <ExceptionMessage>Type 'Helpers.EnumModel' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.</ExceptionMessage> 
    <ExceptionType>System.Runtime.Serialization.InvalidDataContractException</ExceptionType> 

任何想法,爲什麼?

回答

0

我可能是錯的,但InnerException似乎暗示匿名類型不能被序列化。

嘗試聲明一個類如

public class EnumInfo { 
    public int Id { get; set; } 
    public string Description { get; set; } 

    public EnumInfo(int id, string description) { 
     Id = id; 
     Description = description; 
    } 
} 

,並把您的通話Select

[...].Select(x => new EnumInfo((Int32)x, x.Attribute<DescriptionAttribute>().Description); 
+0

我試着用班級,因爲你建議,但我一直有相同的錯誤... –

+0

@MDMoura你可以請編輯你的問題,包括你得到的錯誤*現在*? –

+0

我剛更新了我的問題......我的課似乎很正常,所以我不知道爲什麼我現在得到這個錯誤。 –

2

你怎麼呼喚你的API?它看起來像試圖使用XML格式化程序進行內容協商。但是,XML序列化程序不支持匿名類型。詳情請查閱this link

要解決此問題,您應該發送Accept: application/json頭(如果您使用招或類似這樣的工具),或明確地告訴你只需要JSON格式的Web API:

config.Formatters.Clear(); 
var jsonFormatter = new JsonMediaTypeFormatter 
{ 
    // Use camel case formatting. 
    SerializerSettings = 
    { 
     ContractResolver = new CamelCasePropertyNamesContractResolver(), 
    } 
}; 
config.Formatters.Add(jsonFormatter); 
+0

事實上,我正在調用瀏覽器上的API來測試它...因此,獲取XML是正常的。按照s ..的建議將匿名類型更改爲列表,但我仍然存在相同的錯誤。任何想法爲什麼? –

+0

不應該讓調用者決定哪種類型想要的,而不是強制類型(JSON,XML,...)上的動作? –

+0

@MDMoura,這是內容協商的主要思想;但是,在大多數情況下,只需要JSON格式。 –

相關問題