Web API ODataResult总是序列化为Atom [英] Web API ODataResult Is Always Serializing to Atom

查看:65
本文介绍了Web API ODataResult总是序列化为Atom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从新的

I'm returning an ODataResult from the new Web API OData package as follows:

public ODataResult<Product> Get(ODataQueryOptions options)
{
  var results = (options.ApplyTo(_db.Products) as IQueryable<Product>);
  var count  = results.Count;
  var limitedResults = results.Take(100).ToArray();
  return new ODataResult<Product>(results,null,count);
}

上面的方法很好用,但是无论查询类型如何,它总是返回Atom响应.如何返回JSON?

The above works great but it always returns an Atom response regardless of the query type. How can I return JSON?

我可以看到Atom是ODataMediaTypeFormatter集合中第一个受支持的媒体类型.我很乐意完全删除Atom支持,因为我不需要它.更好的办法是正确协商内容类型.

I can see that Atom is the first supported media type within the ODataMediaTypeFormatter collection. I'd be happy just completely removing Atom support as I don't need it. Even better would be to have the content type correctly negotiated.

推荐答案

您是否尝试过设置accept标头,如下所示:

Have you tried setting the accept header, like this:

Accept = application/json; odata = verbose.

Accept=application/json;odata=verbose.

那应该返回JSON. OData协议已经支持JSON一段时间了,但是在协议的V3中,application/json映射到了称为JSON light的东西(尚未实现).因此,直到支持JSON light来获取JSON为止,您必须更加具体一些,并请求自V1以来OData中已有的更详细的JSON版本.

That should return JSON. The OData protocol has supported JSON for a while but in V3 of the protocol application/json is mapped to something called JSON light (which is not yet implemented). So to until JSON light is supported to get JSON you have to be a little more specific, and request the more verbose version of JSON that has been in OData since V1.

这篇关于Web API ODataResult总是序列化为Atom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆