Asp.Net MVC4的Web API - 返回的OData格式的Json从查询,而无需定制MediaTypeFormatter [英] Asp.Net MVC4 Web API - Return OData formatted Json from query without custom MediaTypeFormatter

查看:172
本文介绍了Asp.Net MVC4的Web API - 返回的OData格式的Json从查询,而无需定制MediaTypeFormatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用新的的WebAPI 公测建立一个Web服务,我可以查询使用的 OData的查询字符串公约并使其返回的 OData的格式化的Json 。我也想用的OData 2.0(而不是1.0)。

虽然它看起来并不像它对于 $选择选项,并支持头似乎能够覆盖 $格式选项,返回一个的IQueryable 通用似乎提供为我最需要的其他查询选项的支持。

我真的很挣扎是如何最好地提供满足的OData 2.0规范JSON对象。查询时的WebAPI只是返回正常的JSON数据。例如,如果我执行这样的一个GET请求......

 的http://本地主机:XXXXX /供应商/ $过滤器= RONUMBER EQ'2597385'和; $选择= VENDORID,VENDORNAME&安培; $顶部= 1&安培; $格式=的Json

..让高层打了我的dev的机器匹配指定RONumber,我得到含用下列JSON的响应...

<$p$p><$c$c>[{\"MEPartNumber\":\"11-2222-3-4444\",\"MFGPartNumber\":\"123456-123\",\"MFGSerialNumber\":\"ABC123\",\"RONumber\":\"987654321\",\"VendorId\":\"1234567\",\"VendorName\":\"ACME服务中心}]

我需要的JSON满足的OData 2.0规范。更多的东西这样的..

 的OData V2:{
  D:{
    结果:{
        __metadata:{
            URI:HTTP:// someserver /富/供应商(1),
            类型:供应商
        },
        MEPartNumber:11-2222-3-4444,
        MFGPartNumber:123456-123,
        MFGSerialNumber:ABC123,
        RONumber:987654321,
        VENDORID:1234567,
        VENDORNAME:ACME服务中心
    }
  }
}

我想我可以写一个自定义 MediaTypeFormatter 来得到我想要的结构。我也许还可以修改我返回的对象,以模仿所需的JSON结构。除了这两个选项,没有人知道的一个聪明的办法,使的WebAPI给我的OData 2.0格式的JSON对象?


解决方案

您需要编写自己的 MediaTypeFormatter 来提供正确的序列化。我们没有出货的OData的格式在MVC 4 Beta版,但WCF的Web API的previous化身对如何写一个例子。 <一href=\"http://wcf.$c$cplex.com/SourceControl/list/changesets\">http://wcf.$c$cplex.com/SourceControl/list/changesets寻找Microsoft.Net.Http.Formatting.OData(你应该能够使用大多数的来源,但一些实施细节可能已经改变了)

另一种方法是,试图构建一个DTO,将序列通过的OData V2所期望的形状。

I am trying to use the new WebAPI beta to build a web service that I can query using the OData query string conventions and have it return OData formatted Json. I also want to use OData 2.0 (instead of 1.0).

While it doesn't look like it has support for the $select option and headers seem to be able to override the $format option, returning an IQueryable generic seems to provide support for most other query options that I need.

What I am really struggling with is how best to provide Json objects that meet the OData 2.0 specification. WebAPI just returns normal Json data when queried. For example, if I perform a GET request of something like this...

http://localhost:XXXXX/vendor/?$filter=RONUMBER eq '2597385'&$select=VENDORID,VENDORNAME&$top=1&$format=Json

..to get the top hit matching the specified RONumber on my dev machine, I get a response containing the following Json...

[{"MEPartNumber":"11-2222-3-4444","MFGPartNumber":"123456-123","MFGSerialNumber":"ABC123","RONumber":"987654321","VendorId":"1234567","VendorName":"ACME SERVICE CENTER"}]

I need Json that meets the OData 2.0 spec. Something more like this..

OData V2: { 
  "d" : { 
    "results":  { 
        "__metadata": {
            "uri": "http://someserver/foo/vendor(1)",
            "type": "Vendor"
        },
        "MEPartNumber": "11-2222-3-4444",
        "MFGPartNumber": "123456-123",
        "MFGSerialNumber": "ABC123", 
        "RONumber":"987654321", 
        "VendorId":"1234567", 
        "VendorName": "ACME SERVICE CENTER"
    }
  }
}

I suppose I could write a custom MediaTypeFormatter to get the structure that I want. I might also be able to modify my returned objects to mimic the structure of the desired Json. Aside from these two options, does anyone know of a clever way to make WebAPI give me OData 2.0 formatted Json objects?

解决方案

You need to write your own MediaTypeFormatter to provide the right serialization. We didn't ship an OData formatter in the MVC 4 Beta, but the previous incarnation of WCF Web Api has some examples of how to write one. http://wcf.codeplex.com/SourceControl/list/changesets Look for Microsoft.Net.Http.Formatting.OData (you should be able to use most of the source, though some implementation details might've changed)

An alternative would be to try to construct a DTO that would serialize to the shape expected by OData v2.

这篇关于Asp.Net MVC4的Web API - 返回的OData格式的Json从查询,而无需定制MediaTypeFormatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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