Get the OData catalog for Web API OData v4 in XML

。_饼干妹妹 提交于 2019-12-21 04:51:42

问题


I am trying to get a Web API OData V4 endpoint up and running.

I got it going finally (after removing all the DateTime properties from my endpoint) and now the listing of the entities is in 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.

I have looked and can't seem to find a way to change this back to XML, so I am asking here.

Is there a way to have the listing of entities for a Web API OData v4 feed be in XML rather than JSON?


回答1:


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:

In WebAPiConfig, add namespace references to:

using System.Net.Http.Formatting;
using System.Web.OData.Formatter;

and then add something like:

var formatters = ODataMediaTypeFormatters.Create();
config.Formatters.InsertRange(0, formatters);

The entity listing now gets returned as xml.

The downside, is now all the responses default to the less preferred, verbose xml/atom.

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)

However, as stated previous, LinqPad still does not recognize the v4 schema, and will not connect correctly to this endpoint.




回答2:


If you have setup a Web API service using the basic, default configurations that the MS walkthroughs suggest, then the response formatter is already configured and will use json by default, or xml if so prompted.

So the prompt for an xml response usually comes from the client request. At its basic form, the request will contain a Accept: application/atom+xml,application/xml header. If it does not, I believe the Web API response will default to json.

For your particular question, there is an alternative for LinqPad. When you setup your OData connection, the dialog has a Formatter option of Xml or Json, which tells Linqpad to issue the corresponding Accept header in its request.

Using Fiddler to monitor the traffic, if you set the LinqPad OData connection to use the xml formatter, the request contains the headers:

MaxDataServiceVersion: 3.0;NetFx
Accept: application/atom+xml,application/xml

and the response comes back as an atom/xml feed.

If you set the option to use the json formatter, the request contains the headers:

MaxDataServiceVersion: 3.0;NetFx
Accept: application/json;odata=minimalmetadata

and the response comes back as json.

For oData v3, LinqPad handles both just fine.

Edit

This now sounds vaguely familiar with some test I ran a short time ago...I'm not sure if LinqPad supports the v4 protocol yet. It uses WCF Data Services client under the covers, and those libraries stopped at OData v3. And, in fact, as you see from the headers above, the request will only talk to a v3 service.

So your issue is not that the Web Api is not supporting both xml and json. The issue is that LinqPad does not yet connect to a v4 oData service. Using Fiddler or other similar tool, you should be able to request both json and/or xml from the service.



来源:https://stackoverflow.com/questions/25210661/get-the-odata-catalog-for-web-api-odata-v4-in-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!