获取XML中的Web API OData v4的OData目录 [英] Get the OData catalog for Web API OData v4 in XML
问题描述
我正在尝试启动并运行Web API OData V4端点.
I am trying to get a Web API OData V4 endpoint up and running.
我终于做到了(从端点中删除了所有DateTime属性后),现在实体列表在JSON中.
I got it going finally (after removing all the DateTime properties from my endpoint) and now the listing of the entities is in JSON.
我喜欢JSON,但是我使用LinqPad来测试我的端点.它不了解供稿中列出的实体的JSON.
I love JSON, but I use LinqPad to test my endpoints. It does not understand the JSON for the listing of the entities that are in my feed.
我已经看过,而且似乎找不到找到将其更改回XML的方法,所以我在这里问.
I have looked and can't seem to find a way to change this back to XML, so I am asking here.
是否可以使用XML而不是JSON来提供Web API OData v4提要的实体列表?
推荐答案
很抱歉再次发布另一个答案,但是我的第一个答案太冗长了. 我找到了此链接: V4始终返回Json ,并且可以肯定的是,最后的建议确实有效:
Sorry to post another answer, but my first one was getting too lengthy. I found this link: V4 always returns Json and sure enough, the very last suggestion does work:
在WebAPiConfig中,将命名空间引用添加到:
In WebAPiConfig, add namespace references to:
using System.Net.Http.Formatting;
using System.Web.OData.Formatter;
,然后添加类似内容:
var formatters = ODataMediaTypeFormatters.Create();
config.Formatters.InsertRange(0, formatters);
实体列表现在以xml返回.
The entity listing now gets returned as xml.
不利的一面是,所有响应现在默认为次要的,冗长的xml/atom.
The downside, is now all the responses default to the less preferred, verbose xml/atom.
好处是,现在在v4中满足了$format
请求.因此,要返回json,您可以将url发出(不弄乱标题):
http://<myodataurl>?$format=application/json;odata.metadata=full
(或最小或无)
The upside, is that the $format
request is now honored in v4. So to get back to json, you can issue the url (without messing with headers) as:
http://<myodataurl>?$format=application/json;odata.metadata=full
(or minimal or none)
但是,如前所述,LinqPad仍然无法识别v4模式,并且将无法正确连接到此端点.
However, as stated previous, LinqPad still does not recognize the v4 schema, and will not connect correctly to this endpoint.
这篇关于获取XML中的Web API OData v4的OData目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!