Our task here is to perform OData protocol queries in order to get the value of some Entity's property, the following query for example :
http://localhost:6954/Odata/Notes(10)/Body/$value
When developing an ODataController to create an OData Web API, having configurated the application properly, as in the previous tutorial, you get automatically support for the most common OData protocol's queries. You can then query the OData Web API using $metadata, $value, $select, $top, $skip, $filter and even $expand , to get the JSON response:
http://localhost:6954/OData/$metadata#Notes :
http://localhost:6954/OData/Notes?$skip=2&$top=10
http://localhost:6954/OData/Notes?$select=Body
There are some conventions to follow when developing an ODataController:
1) the controller's name must be the entity name, the root of the resource path:
/Notes(10) implies that the Web API will look for a controller named NotesController (case sensitive)
2) the Action name for fetching an Entity's property will be set according to the Entity type and the Property name, as follows:
/Notes(10) implies that the Web API will look for a controller named NotesController (case sensitive)
2) the Action name for fetching an Entity's property will be set according to the Entity type and the Property name, as follows:
/Notes(10)/Body
means that the Web API will look for an Action method named GetBodyFromNote()
All this conventions can be found in the Microsoft Asp.Net official site.
Let's say we have an Entity named "Note" with some property called "Body" : we want to call the OData HTTP service with this URI:
http://localhost:6954/Odata/Notes(10)/Body/
So, according to the OData protocol specification, we need an Action method named GetBodyFromNote(), marked with the attribute "[EnableQuery]" , which returns the required property from the Entity:
(COPY-PASTE THIS CODE) :
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
public string GetBodyFromNote([FromODataUri]int key)
{
IEnumerable<Note> data = Repository.GetAll();
var note = data.Where(n => n.ID == key) .SingleOrDefault();
return note.Body ;
}
Build & run the application, and type the URI to fetch the required property in JSON format:
http://localhost:6954/Odata/Notes(10)/Body/
Also, we can request ONLY the $value of the property:
http://localhost:6954/Odata/Notes(10)/Body/$value
That's all
All this conventions can be found in the Microsoft Asp.Net official site.
Let's say we have an Entity named "Note" with some property called "Body" : we want to call the OData HTTP service with this URI:
http://localhost:6954/Odata/Notes(10)/Body/
So, according to the OData protocol specification, we need an Action method named GetBodyFromNote(), marked with the attribute "[EnableQuery]" , which returns the required property from the Entity:
(COPY-PASTE THIS CODE) :
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
public string GetBodyFromNote([FromODataUri]int key)
{
IEnumerable<Note> data = Repository.GetAll();
var note = data.Where(n => n.ID == key) .SingleOrDefault();
return note.Body ;
}
http://localhost:6954/Odata/Notes(10)/Body/
Also, we can request ONLY the $value of the property:
http://localhost:6954/Odata/Notes(10)/Body/$value
That's all
In this post we've seen how we perform Querying Entities' Properties in Web API OData RESTful with ODataController.
Enjoy programming.....
By Carmel Shvartzman
Enjoy programming.....
By Carmel Shvartzman
כתב: כרמל שוורצמן