【问题标题】:My ASP.NET REST app is returning JSON even though the client is requesting XML即使客户端请求 XML,我的 ASP.NET REST 应用程序仍在返回 JSON
【发布时间】:2020-09-12 09:19:48
【问题描述】:

我有一个 ASP.NET Web API2 Web 应用程序。对于它的配置,它有:

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

客户端正在请求 XML:

private static HttpWebRequest CreatetRequest(Uri uri, string method, int timeout)
{
    var request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = method;
    request.Timeout = timeout == 0 ? System.Threading.Timeout.Infinite : timeout;
    request.Accept = "application/xml";
    request.ContentType = "application/xml";
    return request;
}

但是返回的是 JSON。然后当客户端进行以下调用时:

       using (var stream = response.GetResponseStream())
        {
                body = XDocument.Load(XmlReader.Create(stream));

它会引发异常,因为它无法将 JSON 加载为 XML。我需要在服务器端做什么来解决这个问题(我无法更改客户端 - 该代码已失效并正在使用中)?

【问题讨论】:

    标签: asp.net-web-api xml-serialization


    【解决方案1】:

    找到它 - 返回的对象之一 dd 没有无参数构造函数。 XML 序列化需要,而 JSON 不需要。

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 2011-06-13
      • 2017-07-10
      相关资源
      最近更新 更多