简体   繁体   English

如何在OData中排除节点

[英]How to exclude a node in OData

I have a Odata response structure as below, 我有一个如下的Odata响应结构,

{
"value": [{
    "a": "10",
    "b": "10",
    "c": "10",
    "Description": null,
    "ExtendedValues": [{
        "x1": "val1",
        "y1": "val2",
        "z1": "val3"
    },
    {
        "x2": "val1",
        "y2": "val2",
        "z2": "val3"
    },
    {
        "x3": "val1",
        "y3": "val2",
        "z3": "val3"
    },
    ],
}]

} }

I just want to know if there is a way to exclude a specific node from the response. 我只想知道是否有办法从响应中排除特定节点。 For ex i need to exclude ExtendedValues from the response. 例如,我需要从响应中排除ExtendedValues。 Although i could do it by using $select and provide all the required properties. 虽然我可以通过使用$ select并提供所有必需的属性来做到这一点。

There are several ways to do so: 有几种方法可以这样做:

1 Use IgnoreDataMember in ConventionModelBuilder. 1在ConventionModelBuilder中使用IgnoreDataMember。 In the WebApiConfig.cs file WebApiConfig.cs文件中

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntityType<Person>().Ignore(p => p.Description);

Then the property Description will be excluded, BUT it will also disappear in $metadata 然后属性说明将被排除,但它也会在$ metadata中消失

2 You can always make such thing work by defining an unbound function to return the value you like. 2您总是可以通过定义一个未绑定的函数来返回所需的值来使这种事情起作用。 Please refer the sample 请参考样品

3 If you have a lot of business logic, you may try RESTier -- a framework built on Web API OData and makes business logic easy to implement. 3如果您有很多业务逻辑,则可以尝试RESTier-一种基于Web API OData构建的框架,并使业务逻辑易于实现。 More can be seen at http://odata.github.io/RESTier/ . 有关更多信息,请参见http://odata.github.io/RESTier/ You can follow the tutorial of Getting-started 2, entity set filter . 您可以按照入门指南2, 实体集过滤器的教程进行操作。 But currently, RESTier is still a preview version. 但是目前,RESTier仍然是预览版。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM