Webapi 2不返回XML列表

时间:2016-09-07 08:42:34

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

SELECT COUNT(*)

我需要使用webapi2以XML格式输出这个输出;我已经用Google搜索并尝试

public IEnumerable<Word> GetWords()
{
    return db.Words.ToList();
}

它仅以JSON格式返回数据。

我还更新了包var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "text/xml"); config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

如何以XML格式获取它?

提前致谢。

2 个答案:

答案 0 :(得分:0)

将HTTP Accept和Content-Type标头设置为&#34; application / xml&#34;。

答案 1 :(得分:0)

我设置了一个空的Web API项目,并向PostMan发出了一个GET请求。开箱即用的Web API返回了XML,如

 <ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
      <string>value1</string>
      <string>value2</string>
 </ArrayOfstring>

表示默认示例控制器。诀窍是在请求中设置Accept Header。

 Accept: text/xml (or application/xml)

请记住规定的行

config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

删除媒体类型。因此它将禁用XML输出(而不是启用它)。