xml - ASP.NET 网络 API : XMLFormatter thinks he can write Json-objects

标签 xml json asp.net-mvc-4 asp.net-web-api

我有这样的方法:

public JObject Get(int id)
        {
            return new JObject(new JProperty("Test", "Test"));
        }

如果我请求 JSON,它工作正常,但如果我请求 XML,我会从 Web API 框架获得 HTTP-Errorcode 500(无一异常(exception))。 XMLformatter 似乎认为他可以编写 json。 我可以用它来测试:

bool test = GlobalConfiguration.Configuration.Formatters.XmlFormatter.CanWriteType(typeof(JArray));
            bool test2 = GlobalConfiguration.Configuration.Formatters.XmlFormatter.CanWriteType(typeof(JObject));
            bool test3 = GlobalConfiguration.Configuration.Formatters.XmlFormatter.CanWriteType(typeof(JProperty));
            bool test4 = GlobalConfiguration.Configuration.Formatters.XmlFormatter.CanWriteType(typeof(JValue));

它总是返回“真”。 我不想删除 xmlformatter,但服务器抛出我不生成且无法解决的 HTTP 错误 500 是 Not Acceptable 。 最好的是 XMLformatter 可以序列化对象...

最佳答案

DataContractSerializer 不支持匿名/动态类型(甚至是通用“对象”)。这就是它没有序列化为 XML 的原因。

如果你想要 XML 序列化,你必须使用强类型对象。

关于xml - ASP.NET 网络 API : XMLFormatter thinks he can write Json-objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11047683/

相关文章:

java - 将对象集合序列化为 JSON 和 XML

JSON 数组上的 MySQL 通配符

c# - Web Api响应版本控制解决方案?

asp.net-mvc-4 - IIS 模拟返回应用程序池用户服务帐户

java - 无法实例化 MessageBodyReader 错误

xml - XML 模式中的限制

c# - XElement 添加一个 xmlns

php - 如何使用 php pdo 创建 JSON 嵌套对象

Python高效反向列表JSON序列化

c# - 找不到与请求 URI 'Myurl' 匹配的 HTTP 资源 在 Controller 'controllername' 上找不到与请求匹配的操作