Asp.net web api Web API中不应出现XmlMediaTypeFormatter抛出的异常

Asp.net web api Web API中不应出现XmlMediaTypeFormatter抛出的异常,asp.net-web-api,xml-serialization,asp.net-mvc-5,mediatypeformatter,Asp.net Web Api,Xml Serialization,Asp.net Mvc 5,Mediatypeformatter,我正在使用MVC5编写一个诊断网站,它将向多个后端服务发出任意请求,并在浏览器中以Json或XML报告结果 由于结果类型很多,所以我将对象类型的变量传递给Okcontent方法 当我请求Json输出时,它工作正常,但在请求xml输出时,XmlMediaTypeFormatter失败。例外是 The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; char

我正在使用MVC5编写一个诊断网站,它将向多个后端服务发出任意请求,并在浏览器中以Json或XML报告结果

由于结果类型很多,所以我将对象类型的变量传递给Okcontent方法

当我请求Json输出时,它工作正常,但在请求xml输出时,XmlMediaTypeFormatter失败。例外是

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
Type '<type>' with data contract name '<schema-address>' is not expected. 
Consider using a DataContractResolver or add any types not known statically 
to the list of known types - for example, by using the KnownTypeAttribute 
attribute or by adding them to the list of known types passed to
DataContractSerializer.
内部的例外是

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
Type '<type>' with data contract name '<schema-address>' is not expected. 
Consider using a DataContractResolver or add any types not known statically 
to the list of known types - for example, by using the KnownTypeAttribute 
attribute or by adding them to the list of known types passed to
DataContractSerializer.
这似乎发生在任何不是简单纯对象的返回类型上,例如,字符串返回类型将正确显示,但即使是具有2个整数属性和2个日期时间属性的简单类也会以这种方式失败。使用调试器深入研究代码表明,对HttpRequestMessage.CreateResponse的调用知道主体内容的类型,如果尝试像所说的那样进行操作,则效果很好

谷歌搜索这个错误导致了大量关于实体框架导航链接和代理的讨论,但这些都不适用于这里,因为它们是简单的返回类,不涉及EntityFramework或导航引用

有人有线索吗

谢谢