AspNet WebApi OData Learning

Source: Internet
Author: User

OData Introduction: it is a Web protocol for querying and updating data. OData uses web technologies such as HTTP, Atom publish protocol (AtomPub), and JSON to provide access to different applications, services, and storage information. In addition to some basic operations (such as adding, deleting, modifying, and querying), some advanced operations are also provided, similar to the navigation of filtering data and objects. OData extends the above protocols but does not replace them. It can be replaced by XML (ATOM) or JSON, but the important thing about OData is that it complies with the REST principle. In a sense, it is built on the 'simple' rest http service and has a clear goal-to simplify and standardize the way we operate and query data. OData is a good choice if you have been bored with creating search, filter, or paging APIs for your REST service. OData benefits: We use different methods through OData. Instead of creating client signatures and parameters, we asked the following question: "If you process a dataset as the source and define the mode for the most frequently used operations, what is the service interface like query, paging, sorting, new, delete, and update?" This leads to the creation of OData. OData solves the key service design challenges mentioned above. The AspNet WebApi Client 2012 in Visual Studio 5.0 depends on.. Net Framework 4.5 Framework, so you can only download ,.. Net Framework 4.0 AspNet WebApi OData version: http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData.zh-Hans/4.0.30506 $ Filter usage: Return all products with category equal to "Toys ". http://localhost/ Products? $ Filter = Category eq 'toy' Return all products with price less than 10. http://localhost/ Products? $ Filter = Price lt 10 Logical operators: Return all products where price >=5 and price <= 15. http://localhost/ Products? $ Filter = Price ge 5 and Price le 15 String functions: Return all products with "zz" in the name. http://localhost/ Products? $ Filter = substringof ('zz ', Name) Date functions: Return all products with ReleaseDate after 2005. http://localhost/ Products? $ Filter = year (ReleaseDate) gt 2005 $ orderby usage: Sort by price. http://localhost/ Products? $ Orderby = PriceSort by price in descending order (highest to lowest ). http://localhost/ Products? $ Orderby = Price descSort by category, then sort by price in descending order within categories. http://localhost/ Odata/Products? $ Orderby = Category, Price descPageSize usage: [Queryable (PageSize = 10)] public IQueryable <Product> Get () {return products. asQueryable ();} the client clicks the link to filter the next page. To obtain the page number for the client, we must find the result set. This client can use a parameter named "allpages" of $ inlinecount, to obtain the total number of entries. This "allpages" value indicates the total number of result sets contained by the server. The response is sent to the client: {"odata. metadata ":" http://localhost/ $ Metadata # Products "," odata. count ":" 50 "," value ": [{" ID ": 1," Name ":" Hat "," Price ":" 15 "," Category ": "Apparel" },{ "ID": 2, "Name": "Socks", "Price": "5", "Category": "Apparel"},]} // This method is used to process paging data queries and filter. We can easily and flexibly process the data on the client. Public PageResult <Product> Get (ODataQueryOptions <Product> options) {ODataQuerySettings settings = new ODataQuerySettings () {PageSize = 5}; IQueryable results = options. applyTo (_ products. asQueryable (), settings); return new PageResult <Product> (results as IEnumerable <Product>, Request. getNextPageLink (), Request. getInlineCount ();} here is an example of returning Json: {"Items": [{"ID": 1, "Name": "Hat", "Price ": "15", "Category": "Apparel"}, {"ID": 2, "Name": "Socks", "Price": "5", "Category ": "Apparel"}, // Others not shown], "NextPageLink ":" http://localhost/ Api/values? $ Inlinecount = allpages & $ skip = 10 "," Count ": 50}

Related Article

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.