简体   繁体   English

OData V4属性访问

[英]OData V4 property access

I have created a very primitive OData webservice using a Visual Studio 2015 Web API 2 project and a scaffolded controller ("Microsoft OData V4 Web API Controller Using Entity Framework" scaffolding). 我使用Visual Studio 2015 Web API 2项目和脚手架控制器(“Microsoft OData V4 Web API Controller Using Entity Framework”脚手架)创建了一个非常原始的OData Web服务。

I have only one entity set in my model: "Books". 我的模型中只有一个实体集:“书籍”。 The "Book" entity has a privimtive "Title" string property. “Book”实体具有特权“Title”字符串属性。

I presumed that the scaffolded controller would serve all legal OData v4 requests, but that seems not to be the case? 我推测脚手架控制器可以满足所有合法的OData v4请求,但似乎并非如此?

http://localhost:xxxx/OData/Books works (enumerates all books with all properties - including the "Title" property) http:// localhost:xxxx / OData / Books有效(枚举所有属性的书籍 - 包括“Title”属性)

http://localhost:xxxx/OData/Books(1) works (enumerates all properties on the first book, including the "Title" property) http:// localhost:xxxx / OData / Books(1)有效(枚举第一本书上的所有属性,包括“Title”属性)

http://localhost:xxxx/OData/Books(1)/Title does not work - I get a "No routing convention was found to select an action for the OData path with template '~/entityset/key/property" error, despite that the request is 100% OData v4 complaint? http:// localhost:xxxx / OData / Books(1)/ Title 不起作用 - 我得到“没有找到路由约定来为OData路径选择一个带有模板'〜/ entityset / key / property”的错误,尽管该请求是100%OData v4投诉?

Why doesn't it work? 为什么不起作用? Shouldn't it work? 不应该工作吗? What will it take to make it work? 如何使其发挥作用?

The scaffolding does not create a controller that serves all legal OData v4 requests. 脚手架创建一个控制器,为所有合法的OData v4的请求。

To retrieve the Title property of a Book entity, you must add a method to the BooksController as follows: 要检索Book实体的Title属性,必须按如下方式向BooksController添加方法:

public string GetTitleFromBook([FromODataUri] int key) 
{
    // Put appropriate method logic here.
}

See "Querying a Navigation Property" and "Properties" tables in Routing Conventions in ASP.NET Web API 2 Odata . 请参阅ASP.NET Web API 2 Odata中的“ 路由约定”中的 “查询导航属性”和“属性”表。

Also, consider using the $select query option to limit the properties you get in response to a normal request for an entity. 另外,请考虑使用$select查询选项来限制为响应实体的正常请求而获得的属性。 For example: 例如:

GET http://localhost:xxxx/OData/Books(1)?$select=Title

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

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