【问题标题】:The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'“ObjectContent`1”类型无法序列化内容类型“application/xml”的响应正文;字符集=utf-8'
【发布时间】:2015-06-27 19:16:35
【问题描述】:

我正在学习 web.api,并编写测试应用程序。 我在浏览器中收到此错误,同时调用我的新 web.api 方法:

    // GET api/values/5
    public object Get(int id)
    {
        object o = new {name="value", id=id};
        return o;
    }

我在调试模式下没有收到任何错误。

注意:我发现了一堆名称相似的错误,但它们与我的示例相差甚远。

完全错误:

<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__AnonymousType3`2[System.String,System.Int32]' 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>
<StackTrace>
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type) at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type) at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type) at System.Runtime.Serialization.DataContractSerializer.GetDataContract(DataContract declaredTypeContract, Type declaredType, Type objectType) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph) at System.Net.Http.Formatting.XmlMediaTypeFormatter.<>c__DisplayClass7.<WriteToStreamAsync>b__6() at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)
</StackTrace>
</InnerException>
</Error>

【问题讨论】:

    标签: c# .net asp.net-web-api


    【解决方案1】:

    如果您尝试使用 ajax 调用并在浏览器网络上检查该调用,您可以看到结果。我遇到了同样的错误,我找到了这种测试方法。

    问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-07
      • 2017-12-13
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 2020-02-28
      • 2017-02-01
      • 2013-10-01
      相关资源
      最近更新 更多