【问题标题】:Web API returns the JSON but not XmlWeb API 返回 JSON 但不返回 Xml
【发布时间】: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);

当我尝试在 fiddler 客户端中执行 API 时

当我点击 JSON 时,它会显示为 [{"AccountID":"Research","CounterSeq":"3"}]

我有两个问题

  1. 如何在文件名 QueryResult.xml 中返回响应。

  2. 我们如何自动返回 XML 而不是 JSON。我不确定在 Web API 应用程序中在哪里接受内容。

我是 API 新手,不知道我在哪里遗漏了一些东西。非常感谢任何帮助

【问题讨论】:

  • 您的答案之一是#2:stackoverflow.com/questions/9956052/… 我刚刚对其进行了测试,它返回了 XML。对于#1,您可能需要研究如何返回文件,但您可能需要先创建 XML 对象。

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


【解决方案1】:

结果的格式取决于两件事。

  1. 什么是允许的
  2. 请求了什么

允许的序列化程序

允许的内容取决于WebApiConfig.cs 中包含的序列化程序。默认情况下,xml 序列化程序和 json 序列化程序都包括在内。如果您只想为整个 Web 应用程序返回 XML,请像这样删除 json 格式化程序。

GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.JsonFormatter);

客户请求

客户端可以根据http头content-type指定他们想要的内容,其值为application/jsonapplication/xml,具体取决于他们希望返回数据的格式。

如果客户端未在请求的标头中指定任何内容,则使用列表中第一个允许的序列化程序作为默认值。

【讨论】:

    【解决方案2】:

    也许使用 DataSet - 像这样...

    DataSet ds = null;
    using (SqlConnection connection = new SqlConnection(sConnectionString))
    {
        connection.Open();
        SqlCommand Sqlcommand = new SqlCommand();
        Sqlcommand.CommandType = CommandType.StoredProcedure;
        if (SqlParamlist != null)
        {
            foreach (SqlParameter Sqlparameter in SqlParamlist)
            {
                Sqlcommand.Parameters.AddWithValue(Sqlparameter.ParameterName, Sqlparameter.Value);                                
            }
        }
        Sqlcommand.CommandText = sProc;
        Sqlcommand.Connection = connection;
        sProcParams = sProc + " " + sProcParams;
        using (SqlDataAdapter da = new SqlDataAdapter(Sqlcommand))
        {
            ds = new DataSet("DATATABLE");
            da.Fill(ds, "DATAROW");
            Sqlcommand.Parameters.Clear();
            da.Dispose();
        }
        connection.Close();
    }
    XmlDataDocument xDoc = new XmlDataDocument();
    xDoc.LoadXml(ds.GetXml());
    

    【讨论】:

      【解决方案3】:

      ASP.NET MVC (API) 具有用于数据序列化的所谓媒体格式化程序。默认情况下,API 设置为 JSON(我认为来自 MVC API v2)。您可以使用全局配置对象轻松地将默认设置为 XML:

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

      可以在这里找到一个很好的介绍:JSON and XML Serialization in ASP.NET Web API 或在这里:Formatting Response Data。媒体格式化程序是个好东西,因为您可以创建自己的 custom formatters 并在您的 API 中使用它们

      如果您没有找到全局配置,请查看this SO post

      【讨论】:

        【解决方案4】:

        如何在文件名 QueryResult.xml 中返回响应。

        您的意思是将响应保存在xml 文件中吗?好吧,无论如何我想你知道该怎么做,我认为真正的问题是第二个问题。

        我们如何自动返回 XML 而不是 JSON。我不确定在 Web API 应用程序中在哪里接受内容。

        在您的客户端中,只需将其设置为application/xml,然后您的 API 就会以 xml 格式返回内容。

        对不起,如果我确实跳过了一些东西,我看不到你的屏幕截图,防火墙在工作。

        P.S:我只是作为一个答案而不是一个简单的评论来回复,因为我没有足够的声誉来发表一个简单的评论。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-14
          • 1970-01-01
          • 2013-08-18
          • 2013-12-29
          • 2019-12-17
          • 2020-03-17
          • 1970-01-01
          • 2016-10-05
          相关资源
          最近更新 更多