【发布时间】:2013-01-01 19:55:24
【问题描述】:
我刚刚切换到使用适用于 MVC 4 的新 Web api,我在反序列化来自我的 Web 服务的响应时遇到了很大的困难。
这是我的服务器代码:
IEnumerable<Fish> myList = GetFish();
return Request.CreateResponse(HttpStatusCode.OK, myList,"application/xml");
这被序列化为:
<ArrayOfFish xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/MySolution.NameSpace.Resources\" />
因为没有返回结果。
当我尝试使用 XmlMediaTypeFormatter 解析它时,我收到一条错误消息,指出它没有预料到它会得到什么。
我的反序列化代码与我开始使用 web api 之前没有变化,只是:
return content.ReadAsAsync<T>(formatters).Result;
formatters 是一个List 的格式化程序,只包含XmlMediaTypeFormatter
T 是Fish 的通用列表
如果我省略 "application/xml" 类型,那么它似乎以 JSon 发送(因为结果是 [])但是客户端需要 xml,并且由于某种原因不会在其上使用 JSon 序列化程序,即使我明确输入类型为text/json。
反序列化对象应该相当简单,因为这正是我之前所做的,我只是稍微更改了我的服务器以使用CreateResponse 来创建HttpResponseMessage,而不是直接使用HttpResponseMessage<T>,因为不再支持通用版本。我在网上找不到单个客户端/服务器示例,有人将结果解码为令人沮丧的对象。
有什么想法吗?
【问题讨论】:
标签: asp.net-mvc-4 asp.net-web-api