我有这样的方法:
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/