Web API - “对象内容类型无法序列化
本文关键字:类型 序列化 API 对象 Web | 更新日期: 2023-09-27 18:30:15
我有以下Web API操作。
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>
为什么?
更新
我现在有:
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>
知道为什么吗?
你如何调用你的API?看起来它正在尝试使用 XML 格式化程序进行内容协商。但是,XML 序列化程序不支持匿名类型。查看此链接了解详细信息。
若要修复此行为,应发送Accept: application/json
标头(如果您使用的是 Fiddler 或类似工具),或者显式告知 Web API 您只需要 JSON 格式化程序:
config.Formatters.Clear();
var jsonFormatter = new JsonMediaTypeFormatter
{
// Use camel case formatting.
SerializerSettings =
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
}
};
config.Formatters.Add(jsonFormatter);
我可能是错的,但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);