I. Overview
time is ample, take some time to summarize the common usage of OData, Open Data Protocol (OData) is a Web protocol that queries and updates data. OData applies web technologies such as HTTP, Atom Publishing Protocol (ATOMPUB), and JSON to provide access to information for different applications, services, and storage. In addition to providing some basic operations (like adding additions and deletions), it also provides some advanced operations like filtering data and navigating entities. OData extends the above protocols but does not replace them. He can be replaced by XML (ATOM) or JSON but the importance of OData is that it conforms to the rest principle. In a sense, it is built on the ' simple ' rest HTTP service and has a clear purpose-to simplify and standardize the way we manipulate and query data. OData is a good choice if you've been having trouble creating search, filtering, or paging APIs for your rest service.
Second, the WEB API uses:
1. Get Microsoft.AspNet.WebApi.OData third-party plug-ins through NuGet Dll,microsoft.aspnet.webapi.odata provides a range of classes to extend the Web API.
2, webapiconfig The following code to add, open OData support;
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web.Http;usingSystem.Net.Http.Formatting;usingSystem.Web.Http.OData.Extensions;namespacemymvcodata{ Public Static classWebapiconfig { Public Static voidRegister (httpconfiguration config) {//Web API RoutingCONFIG. Maphttpattributeroutes (); Config. Routes.maphttproute (Name:"Defaultapi", Routetemplate:"Api/{controller}/{id}", defaults:New{id =routeparameter.optional}); //Enable OData queriesConfig. Formatters.JsonFormatter.AddQueryStringMapping ("$format","JSON","Application/json"); Config. Formatters.XmlFormatter.AddQueryStringMapping ("$format","XML","Application/xml"); //CONFIG. Enablequerysupport ();CONFIG. Addodataqueryfilter (); //CONFIG. Settimezoneinfo (timezoneinfo.local); } }}
Note:
Add a namespace: using System.Net.Http.Formatting;
Config. Formatters.JsonFormatter.AddQueryStringMapping ("$format", "json", "Application/json");
Config. Formatters.XmlFormatter.AddQueryStringMapping ("$format", "xml", "Application/xml");
Function Description: This code supports WEBAPI data interface display can be displayed in XML or in JSON format, when the call is as follows:
http://localhost:port/api/ProjectManagent? $format =json Show results in JSON format here
Http://localhost:port/api/ProjectManagent? $format =xml Show results here in XML format
3. Write Interface API Control
Add the control code as follows:
usingMymvcodata.models;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Http;usingSystem.Web.Http;usingSystem.Web.Http.OData;usingSystem.Web.Http.OData.Query;namespacemymvcodata.controllers{ Public classMeetingscontroller:apicontroller {Private ReadOnlyIlist<meeting>_scheduledmeetings; PublicMeetingscontroller () {_scheduledmeetings=NewList<meeting> { NewMeeting {Id ="1", Leader ="Mark Nichols", meetingdate =NewDateTime ( -,1, -), Title ="Project X Planning" }, NewMeeting {Id ="3", Leader ="Jim Phelps", meetingdate =NewDateTime ( -,2,8), Title ="Mission Discussion" }, NewMeeting {Id ="6", Leader ="Barney Collier", meetingdate =NewDateTime ( -,3, A), Title ="Advanced Device Technology" }, NewMeeting {Id ="7", Leader ="Willie Armitage", meetingdate =NewDateTime ( -,5, -), Title ="maintaining a strong presence" }, NewMeeting {Id ="9", Leader ="Cinnamon Carter", meetingdate =NewDateTime ( -,2, the), Title ="Company Fashion" }, NewMeeting {Id ="Ten", Leader ="Rollin Hand", meetingdate =NewDateTime ( -,1, +), Title ="Magic Selling" } }; } //GET api/meeting//[Enablequery (allowedqueryoptions = allowedqueryoptions.all)]//[queryable (allowedqueryoptions = allowedqueryoptions.all)]//[Enablequery (allowedqueryoptions = Allowedqueryoptions.top | ALLOWEDQUERYOPTIONS.SKIP)]//[Enablequery (maxtop = 100)] Configuration call Limit item[Enablequery (allowedqueryoptions = Allowedqueryoptions.all,maxtop = -, Maxskip = $, PageSize =2, allowedfunctions =Allowedfunctions.all)] PublicIqueryable<meeting>Get () {return_scheduledmeetings.asqueryable (); } }}
4. Interface Invocation Description:
The following table lists some common OData operations:
Operation |
Url |
Description |
$filter |
Http://localhost:8090/api/Meetings? $filter =productname eq ' Tofu ' |
Returns results based on the state of an expression (returns ProductName equals Tofu) |
$orderby |
Http://localhost:8090/api/Meetings? $orderby =productname |
Sort by results (sort by ProductName column) |
$skip |
Http://localhost:8090/api/Meetings? $skip =10 |
Crossing n data in results, often used for paging |
$top |
Http://localhost:8090/api/Meetings? $top =10 |
Returns the first N records in the result, often used for paging |
$select |
Http://localhost:8090/api/Meetings? $filter =productname eq ' Tofu ' & $select =productname,unitprice |
Select the properties you want to return |
$expand |
Http://localhost:8090/api/Meetings? $expand =supplier |
Returns the navigation Properties (association properties) contained in the products Supplier |
$inlinecount |
Http://localhost:8090/api/Meetings? $inlinecount =allpages |
The total number of resources that are eligible to be fetched to the server (overall value of pagination) |
Through the table above, we can also combine query conditions to implement complex queries.
Examples of common queries:
Example 1: List all product
URL:http://localhost:8914/Products
Example 2, querying products, listing only Name,price examples
URL:http://localhost:8914/Products? $select =name,price
Example 3: List products (only column Name,price), including supplier
URL:http://localhost:8914/Products? $select =name,price& $expand =supplier
Example 4: Filter products to show only data classified as test
URL:http://localhost:8914/Products? $filter =category eq ' Test '
Example 5: Filter products, show only data classified as test, and sort
URL:http://localhost:8914/Products? $filter =category eq ' Test ' & $orderby =price desc
Other ways to use the $filter:
1. http://localhost/Products? $filter =category eq ' Test '
Filter Category=test
2.http://localhost/products? $filter =price LT 10
Filter price less than 10
3.http://localhost/products? $filter =price ge 5 and Price le 15
Filter 5<=price>=15
4. You can also use database functions such as:
$filter =substringof (' zz ', Name)
$filter =year (releasedate) GT 2005
5. About sorting:
$orderby =price
$orderby =price desc
$orderby =category,price desc
6. There are also some filters such as:
$skip, $top, $inlinecount, etc.
Iv. Reference Blog:
Http://www.cnblogs.com/liuju150/p/4602715.html
Http://www.cnblogs.com/shanyou/archive/2013/06/11/3131583.html
V. Follow-up questions:
For the output of the query result time, the current output format is:
"Meetingdate": "2016-08-27t08:27:46.2664375+08:00"
such as the date of the output format is our custom, such as: 2016-08-27 08:27:46
SOURCE download
The ODATA WEB API extension uses