OData 会忽略某些属性,除非明确声明要返回
我的这个模型具有以下属性。 (简化)
public class Blog {
private string Code { get; set; }
private string Name { get; set; }
private byte[] Image { get; set; }
}
当我向 OData URL 发出请求时,例如:http://localhost/api/odata/Blog,我只想返回 Code 和 Name 属性,而忽略图像。如果我做 像 http://localhost/api/odata/Blog?$select=(Code,Name,Image) 这样的请求然后我希望返回图像。我怎样才能做到这一点? 使用 [IgnoreDataMember]
等属性会使 OData 查询无法访问,因此这不是一个合适的解决方案。
I have this model with following attributes. (Simplified)
public class Blog {
private string Code { get; set; }
private string Name { get; set; }
private byte[] Image { get; set; }
}
When I make a request to the OData URL for ex: http://localhost/api/odata/Blog, I want only Code and Name properties to be returned, ignoring the Image. And if I make
a request something like http://localhost/api/odata/Blog?$select=(Code,Name,Image) then I want the Image to be returned. How can I make this work?
Using attributes like [IgnoreDataMember]
makes it unavailable for OData query to be accessed, therefore it is not a suitable solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,Blog 类的属性可能是公共的,而不是私有的。
我有一个类似的场景并通过实现自定义序列化器来解决它:
序列化器提供程序类:
序列化器类:
当然配置:
First, probably properties of the Blog class are public, not private.
I had a similar scenario and resolve it by implementing a custom serializer:
Serializer provider class:
Serializer class:
And configuration of course: