OData with List

Source: Internet
Author: User

What is OData

Official explanation: The Open data Protocol (OData) is a data access Protocol for the web. OData provides a uniform to query and manipulate data sets through CRUD operations (create, read, update, and delete).

Getting Started with OData

To use OData, you have the following work to do.

A. Install the OData package. Can be installed through NuGet Package Manager, install command: Install-package Microsoft.AspNet.Odata

B. Configure the OData EndPoint. Register the OData route in the Webapiconfig register method and create the EDM, as in the code.

C. Create an OData controller and an action with code such as.

You can now access the following two URLs to see the effect.

Http://localhost.dev.wingontravel.com/HWAODataSite/opi? $metadata

, view metadata, such as

Http://localhost.dev.wingontravel.com/HWAODataSite/opi/Citys? $select =cityname,cityenglishname, Query CityName and Cityenglishname, with multiple fields separated by numbers, such as.

Inquire

Let's take a look at what query options are supported by OData, summarized as, and then explained and demonstrated separately.

① $select, typically used to return a specified field, such as Url:http://localhost.dev.wingontravel.com/hwaodatasite/opi/citys? $select =cityname,cityenglishname

② $filter, generally used for filtering results. As in the example below.

Http://localhost.dev.wingontravel.com/HWAODataSite/opi/Citys? $filter =citycode eq ' TSN ', filtering citycode= "TSN"

Http://localhost.dev.wingontravel.com/HWAODataSite/opi/Citys? $filter =cityid Lt 5, filter cityid<5

Http://localhost.dev.wingontravel.com/HWAODataSite/opi/Citys? $filter =cityid GE 5, filter cityid>5

Http://localhost.dev.wingontravel.com/HWAODataSite/opi/Citys? $filter =substringof (' t ', citycode), filtering Citycode contains "t "An error ' an unknown function with name ' Substringof ' is found ', still don't know why.

IMPORTANT: To use the filter function, you must activate it . The following code.

③ $skip and $top, generally used for paging.

? $skip and $top are based on client paging, such as Url:http://localhost.dev.wingontravel.com/hwaodatasite/opi/citys $top =5& $skip =5

? [PageSize] property, service-side paging, the following code, restricts the interface to return only 10 records at a time.

④ erby, sort. Like what

Http://localhost.dev.wingontravel.com/HWAODataSite/opi/Citys? erby = Cityid desc, descending by Cityid

Http://localhost.dev.wingontravel.com/HWAODataSite/opi/Citys? erby = Citycode,cityid desc, combined sort

IMPORTANT: To use order BY, you need to activate. the following code.

3.1 An example of a comprehensive query

Requirements: Query cityid<16, take the second page (5 data per page), sort in descending order of Cityid, and return only city information for the Cityname+cityenglishname two fields.

Url:

Http://localhost.dev.wingontravel.com/HWAODataSite/opi/Citys? $filter =cityid lt 16& $top =5& $skip =5&$ Orderby=cityid desc& $select =cityname,cityenglishname

Note: This URL needs to be encode in fiddler

OData V4 Attribute Routing

The attribute routing for OData V4 is similar to that of Webapi 2, where you can add Odatarouteprefix to the controller and add Odataroute attributes to the action. They are all under the namespace System.Web.OData.Routing, and the Webapi 2 feature is routed under the namespace System.Web.Http. The following code.

Attached: The above demo has been put on GitHub.

1.HTTPS://GITHUB.COM/MCGRADY525/HELLOWEBAPI, routing, serialization, and model binding

2.https://github.com/mcgrady525/hellowebapi.odata, OData and the focus of attention

OData with List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.