【发布时间】:2016-12-22 04:42:54
【问题描述】:
Web API 不返回 XML,而是只返回 JSON
[HttpGet]
public IHttpActionResult Get(string Account)
{
// other code fo connecting to the SQL seerver and calling the stored procedure
reader = command.ExecuteReader();
List<QueryResult>qresults = new List<QueryResult>();
while (reader.Read())
{
QueryResult qr = new QueryResult();
qr.AccountID = reader["AccountID"].ToString();
qr.CounterSeq = reader["CounterSeq"].ToString();
qresults.Add(qr);
}
DbConnection.Close();
return Ok(qresults);
当我点击 JSON 时,它会显示为 [{"AccountID":"Research","CounterSeq":"3"}]
我有两个问题
如何在文件名 QueryResult.xml 中返回响应。
我们如何自动返回 XML 而不是 JSON。我不确定在 Web API 应用程序中在哪里接受内容。
我是 API 新手,不知道我在哪里遗漏了一些东西。非常感谢任何帮助
【问题讨论】:
-
您的答案之一是#2:stackoverflow.com/questions/9956052/… 我刚刚对其进行了测试,它返回了 XML。对于#1,您可能需要研究如何返回文件,但您可能需要先创建 XML 对象。
标签: c# xml asp.net-mvc asp.net-web-api