没有 DataContract 的 WEB API 中的内容协商

Content negotiation in WEB API without DataContract

如何使 HttpResponseMessage 遵循 xml 架构而不是数据合同?

我试过

return new HttpResponseMessage()
{
     StatusCode = HttpStatusCode.OK,
     Content = new ObjectContent<Foo>(foo, Configuration.Formatters.XmlFormatter)

};

但这并没有奏效。

此外,如果我这样做,Action 将始终提供 xml 并且不会进行内容协商。我尝试将 List 添加为参数,但 HttpResponseMessage 不需要列表,只是一个项目。

有没有办法在不使用 DataContract 的情况下进行内容协商?基本上现在 HttpResponseMessage 忽略了 xml 模式。

我猜答案是使用

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;