Problem
How the Web API supports common OData system query entries, such as $select or $filter.
Solution Solutions
In order to enable query entries in the Web API, we need to use Enablequeryattribute on the Action.
If the Action does not return a collection, but instead returns an instance of a single object, the caller can still use the $expand and $select two query statements, and for that purpose we must wrap the returned object in Singleresult<t>. Examples of collections and individual object instances as return values are shown in order 12-7
Listing 12-7. Enable a query statement on two routes
1 Public classPlayerscontroller:odatacontroller2 3 {4 5 Private ReadOnlyPlayerscontext Playersdbcontext =NewPlayerscontext ();6 7 [Enablequery]8 9 PublicIqueryable<player>getallplayers ()Ten One { A - Return Playersdbcontext; - the } - - [Enablequery] - + PublicSingleresult<player> Getsingleplayers (intkey) - + { A at returnSingleresult.create (playersdbcontext.where (x = X.id = =key). AsQueryable ()); - - } - -}
Working principle
The OData query option is defined in the OData specification, such as the parameters of the query string that control the number and order of the returned resources. The ASP. NET Web API supports almost all standard query entries:
- $expand: The information that is allowed to respond to the client contains associated resources (navigation properties)
- $select: Limit the properties returned
- $filter: Filtering out resources exposed by API
- $count: Gets the total number of entities in the collection
- $orderby: Specify the sort key for the collection
- $skip: Gets the quantity skipped by the collection
- $top: Limit Collection returns the number of collections
- $format: Request a specific response format
The only thing that is not supported is $search query parameters.
Tip You can view the full document address supported by OData 4.0: http://docs.oasis-open.org/OData/new-in-OData/v4.0/cn01/new-in-OData-v4.0-cn01.html
The query item is described by the Odataqueryoption class in the ASP. NET Web API. The way the Enablequeryattribute works is actually very simple. Because he is the Filter for action, the response of the action is obtained in the OnActionExecuted method, and the httpcontent is converted to objectcontent<t>. The Odataqueryoptions instance is then constructed based on the client-side parameters of HttpRequest and the corresponding response is returned to the client. If the response is a collection, you can respond to all of the client's query items. If the response is not a collection, it is packaged into singleresult<t> by Objectcontent. At these times, both $expand and $select are available.
If you don't use Enablequeryattribute, you can also use odataqueryoptions, which is acceptable for the Action parameter. This approach is supported by customizing the httpparameterbinding type of odataqueryparameterbinding. We can also use the information in the Odataqueryoptions instance to manually execute the related query.
In the Web API, we don't need to fully master OData to realize the convenience of querying items. Even if it is not based on odatacotrollers, as long as the Enablequeryattribute property tag is used on the Action, we can still use some underlying query items on the Web API, such as $top, $skip, $select.
Code Demo
Use Enablequeryattribute on the Controller's Action, such requests can use OData query items, such as listing 12-7
- Host/plays (1) $select =name,team: Filters with the Id of the Player entity, returning only Name and Team two properties. The response to this example is shown in Listing 12-8.
Listing 12-8. Query Response example from OData Web API
{ "@OData. Context": "http://localhost:43539/OData/$metadata #players (name,team)/$entity", "Name": "Name1", "Team": "Team"}
- host/players?skip=1& $top = 2: Ignores the first entity in the collection, and then gets two back in the remaining entities (the second page of data that can be understood as paging, with a size of 2 per page). The specific response, as shown in Listing 12-9.
Listing 12-9. Query Response example from OData Web API
{ "@OData. Context": "http://localhost:43539/OData/$metadata #players", "value": [ { "Id": 1, "Name": " Name1 "," Team ":" Team " },{ " Id ": 2," Name ":" Name11 "," Team ":" Team " } ]}
- Host/players? $format =application/json;odata.metadata=full& $filter =team%20eq%20%27team2%27: Request condition You are the Team property value of Whales, returned in JSON format, contains all the information for the OData metadata, including type and navigation links. Specific response results, as shown in Listing 12-10
{ "@OData. Context": "http://localhost:43539/OData/$metadata #players", "value": [ { "@OData. Type": "# BoiledCode.WebApi.Recipe.ODataDemo.Models.Player "," @OData. Id ":" http://localhost:43539/OData/Players (3) "," @ Odata.editlink ":" Http://localhost:43539/OData/Players (3) "," Id ": 3," Name ":" Name2 "," Team ":" Team2 " },{ " @ Odata.type ":" #BoiledCode. WebApi.Recipe.ODataDemo.Models.Player "," @OData. Id ":" http://localhost:43539/OData/ Players (4) "," @OData. Editlink ":" Http://localhost:43539/OData/Players (4) "," Id ": 4," Name ":" Name21 "," Team ":" Team2 " } ]}
As mentioned above, we can manually implement the inquiry by using the Odataqueryoptions parameter in the Action method. Odataqueryparameterbinding will help me to get the object into action, so we can extract the OData-related query items inside the action.
Listing 12-11. Using Odataqueryoptions in action
1 PublicIqueryable<player>getallplayers (odataqueryoptions queryoptions)2 3 {4 5 //client-passed top and skip6 7 varFiltered =db. Players.skip (queryOptions.Skip.Value);8 9 if(QueryOptions.Top.Value >0)Ten One { A -Filtered =filtered. Take (queryOptions.Top.Value); - the } - - returnfiltered. AsQueryable (); - +}
[Boiled ASP. NET Web API2 methodology] (12-3) OData query