【问题标题】:Web API Odata not returning correct metadataWeb API Odata 未返回正确的元数据
【发布时间】:2015-04-03 18:05:41
【问题描述】:

我正在使用带有 Web API 的 OData v4 与我的 AngularJS Web 应用程序进行通信。

更具体地说,我正在尝试使用 Kendo UI Grid 显示我的数据。

我的问题是,我的 Web API 没有返回正确的元数据,导致 Kendos 数据源无法显示数据。

我正在进行分页,为此我需要在响应中添加“count”属性,以便 Kendo UI Grid 数据源能够正常工作。

我期望 Web API 的响应应该是这样的: http://docs.oasis-open.org/odata/odata-json-format/v4.0/errata02/os/odata-json-format-v4.0-errata02-os-complete.html#_Toc403940644

但是,我在响应中看到的结果是:

{
  "@odata.context":"http://localhost:1983/odata/$metadata#TestReports","value":[
    {
      "Id":1,"Name":"Test Testesen","Purpose":"Kendo UI Grid Test","Type":"Rumraket","ReportedDate":"2015-02-04T10:03:59.4173323+01:00"
    },{
      "Id":2,"Name":"Gunner Testesen","Purpose":"OData Web API Test","Type":"Sutsko","ReportedDate":"2015-02-04T10:03:59.4173323+01:00"
    },{
      "Id":3,"Name":"Bertram Didriksen","Purpose":"Server Paging Test","Type":"Flyver","ReportedDate":"2015-02-04T10:03:59.4173323+01:00"
    },{
      "Id":4,"Name":"Oluf Petersen","Purpose":"Test","Type":"B\u00e5d","ReportedDate":"2015-02-04T10:03:59.4173323+01:00"
    },{
      "Id":5,"Name":"Alfred Butler","Purpose":"Opvartning","Type":"Batmobil","ReportedDate":"2015-02-04T10:03:59.4173323+01:00"
    }
  ]
}

我检索数据的代码是:

$scope.pendingReports = {
                dataSource: {
                    type: "odata",
                    transport: {
                        read: {
                            beforeSend: function (req) {
                                req.setRequestHeader('Accept', 'application/json;odata=fullmetadata');
                            },
                            url: "/odata/TestReports",
                            dataType: "odata"                                    
                        },
                        parameterMap: function (options, type) {                            
                            var paramMap = kendo.data.transports.odata.parameterMap(options);

                            console.log(paramMap);

                            delete paramMap.$inlinecount; // <-- remove inlinecount parameter
                            delete paramMap.$format; // <-- remove format parameter

                            console.log(paramMap);

                            return paramMap;
                        }
                    },
                    schema: {
                        data: function (data) {
                            return data; // <-- The result is just the data, it doesn't need to be unpacked.
                        },
                        total: function (data) {
                            return data.length; // <-- The total items count is the data length, there is no .Count to unpack.

                        }
                    },
                    pageSize: 5,
                    serverPaging: true,
                    serverSorting: true
                },
                sortable: true,
                pageable: true,
                dataBound: function () {
                    this.expandRow(this.tbody.find("tr.k-master-row").first());
                },
                columns: [
                    {
                        field: "Name",
                        title: "Navn"
                    }, {
                        field: "ReportedDate",
                        title: "Indberetet den"
                    }, {
                        field: "Purpose",
                        title: "Formål"
                    }, {
                        field: "Type",
                        title: "Type"
                    }, {
                        field: "options",
                        title: "Muligheder"
                    }
                ]
            };

我的 WebApiConfig 类是这样的:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            config.Formatters.InsertRange(0, ODataMediaTypeFormatters.Create());

            config.MapODataServiceRoute(
                routeName: "odata",
                routePrefix: "odata",
                model: GetModel()
                );
        }

        public static Microsoft.OData.Edm.IEdmModel GetModel()
        {
            ODataModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet<TestReport>("TestReports");

            return builder.GetEdmModel();
        }
    }

有人对我如何让 Web API 返回正确的元数据有任何建议吗?

【问题讨论】:

    标签: json angularjs asp.net-web-api kendo-grid odata


    【解决方案1】:

    显然 Kendo UI Grid 不支持 OData v4。

    修复方法是修改 Kendo 数据源的 parameterMap,并告诉它使用 $count 而不是 $inlinecount。

    除此之外,我必须告诉架构将“@odata.count”读取为“总计”值。

    在我将之前发布的代码编辑到下面的 ode 之后,我在回复中得到了正确的数据:

    $scope.pendingReports = {
                    dataSource: {
                        type: "odata",
                        transport: {
                            read: {
                                beforeSend: function (req) {
                                    req.setRequestHeader('Accept', 'application/json;odata=fullmetadata');
                                },
                                url: "/odata/TestReports",
                                dataType: "json"                                    
                            },
                            parameterMap: function (options, type) {                            
                                var d = kendo.data.transports.odata.parameterMap(options);
    
                                delete d.$inlinecount; // <-- remove inlinecount parameter                                                        
    
                                d.$count = true;
    
                                return d;
                            }
                        },
                        schema: {
                            data: function (data) {
                                return data.value; // <-- The result is just the data, it doesn't need to be unpacked.
                            },
                            total: function (data) {
                                return data['@odata.count']; // <-- The total items count is the data length, there is no .Count to unpack.
                            }
                        },
                        pageSize: 5,
                        serverPaging: true,
                        serverSorting: true
                    },
                    sortable: true,
                    pageable: true,
                    dataBound: function () {
                        this.expandRow(this.tbody.find("tr.k-master-row").first());
                    },
                    columns: [
                        {
                            field: "Name",
                            title: "Navn"
                        }, {
                            field: "ReportedDate",
                            title: "Indberetet den"
                        }, {
                            field: "Purpose",
                            title: "Formål"
                        }, {
                            field: "Type",
                            title: "Type"
                        }, {
                            field: "options",
                            title: "Muligheder"
                        }
                    ]
                };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 2018-10-27
      相关资源
      最近更新 更多