我怎样才能避免406接收OData.PageResult&LT时,T&GT ;? [英] How can I avoid a 406 when receiving an OData.PageResult<T>?

查看:435
本文介绍了我怎样才能避免406接收OData.PageResult&LT时,T&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回PageResult的ODataController。

I have an ODataController that is returning a PageResult.

API示例:

public PageResult<Customer> Get(ODataQueryOptions options) {
// cut some stuff out...

    PageResult<Customer> result = new PageResult<Customer>(
        searchResults as IEnumerable<Customer>,
        Request.GetNextPageLink(),
        Request.GetInlineCount());
    return result;

当我调试这个,这似乎是罚款,有一个PageResult类建立正确返回。在Web端。

When I debug this, it seems to be fine and have a PageResult class built up correctly to return. On the Web side..

网络示例

using (var client = new HttpClient()) {
    client.BaseAddress = new Uri(testURL);
    string searchUrl = "api/customer?$top=1&$skip=0";
    client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata=verbose"));
    HttpResponseMessage response = client.GetAsync(searchUrl).Result;

响应是一个状态code 406,具有原因短语说明内容是不能接受的。它也做到这一点,如果我定义一个新的MediaTypeWithQualityHeaderValue(应用/ JSON)。

The response is a StatusCode 406, with a reason phrase stating the content was not acceptable. It also does this if I define a new MediaTypeWithQualityHeaderValue("application/json").

什么我需要将它传递给视图之前改变让我成功地消耗这个API中的控制器?

What do I need to change so that I successfully consume this Api in the controller before passing it on to the view?

推荐答案

我认为你缺少构建OData服务的前两个步骤。 ODataController,正如名字一样,仅适用于OData的路线。你需要建立一个EDM模型重新presenting您的OData服务,并添加一个OData的途径暴露的EDM模型。请参阅此官方文档和博客<一href=\"http://blogs.msdn.com/b/webdev/archive/2013/01/29/getting-started-with-asp-net-webapi-odata-in-3-simple-steps.aspx\"相对=nofollow>帖子关于如何建立的OData服务的详细信息。

I think you are missing the first two steps of building an OData service. ODataController, as the name says, only works with OData routes. You need to build an EDM model representing your OData service, and, add an OData route exposing that EDM model. Refer to this official documentation and blog post for details on how to build OData services.

这篇关于我怎样才能避免406接收OData.PageResult&LT时,T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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