Use ASP. NET Web Api to build a REST-based service practice series [7]-implement resource Paging

Source: Internet
Author: User

Series Navigation Address http://www.cnblogs.com/fzrain/p/3490137.html

In this article, we will use different methods to implement manual paging (this article does not cover high-end atmospheric OData, but it may be introduced later in the series, not sure yet ...), For paging results, we will respond to the client in two different ways (1. encapsulate paging metadata in the response Body; 2. Add paging information in the http Response Header ).

As we all know, it is terrible to return hundreds of data records at a time on the server side. When we design an Api, we should return the Get method by page. For example, each time you send a response to 10 pieces of data to the client and contain the "Previous Page" and "next page" tags, the user can obtain the desired data.

Modify the "CoursesController" Get method to implement pagination instead of returning all data at once. The following code is used:

 Object Get( page = ,  pageSize = <Course>= TheRepository.GetAllCourses().OrderBy(c => totalCount = totalPages = ()Math.Ceiling(()totalCount / urlHelper =  prevLink = page >  ? urlHelper.Link(,  { page = page -  }) :  nextLink = page < totalPages -  ? urlHelper.Link(,  { page = page +  }) :  results =*=> =====

Explain the above Code:

Two Parameters with default values are added to the Get method. These two parameters are used to filter the query result set. For example, we want to obtain the data on the 2nd page, the corresponding Get request should be in this form: http: // localhost: {your_port}/api/courses /? Page = 1. Note: Here we only provide one parameter, so pageSize is the default value of 10.

Some responses received by the client should be:

"totalCount": 32"totalPages": 4"prevPageLink": "http://localhost:3300/api/courses?page=0&pageSize=10""nextPageLink": "http://localhost:3300/api/courses?page=2&pageSize=10""results""id": 11"url": "http://localhost:3300/api/courses/11""name": "English Education 2""duration": 4"description": "The course will talk in depth about: English Education 2""tutor""id": 4"email": "Kareem.Ismail@outlook.com""userName": "KareemIsmail""firstName": "Kareem""lastName": "Ismail""gender": 0"subject""id": 4"name": "English"

The returned value of GetAllCourses in Repository is IQueryable. Therefore, when the skip and take methods are executed, SQL statements are not executed in SQL Server. The last query is paging data, shows the characteristics of on-demand query.

 

In the data we return to the client, the paging metadata contains the data totalCount, totalPages, prevPageLink, and nextPageLink. It is very useful for the client to return the data totalCount and totalPages, in this way, you can bind the results with some grids.

 

Generally, we encapsulate paging metadata in the response Body. for developers, we provide all paging information. However, some API consumers only want to obtain the requested data without paging metadata. Therefore, it will be very difficult for them to parse the response results, therefore, there is another way to return paging metadata to the client -- append paging metadata to the Response Message Header: The Body contains only the requested resources, we add the header information "X-Pagination ".

We modify StudentsController to encapsulate the paging metadata in the Header. After this method is used, the client of the paging metadata needs to directly obtain the metadata from the Header, and the client that does not need the metadata can directly parse the response Body.

The implementation is also very simple. The following code:

 IEnumerable<StudentBaseModel> Get( page = ,  pageSize = <Student>= TheRepository.GetAllStudentsWithEnrollments().OrderBy(c => totalCount = totalPages = ()Math.Ceiling(()totalCount / urlHelper =  prevLink = page >  ? urlHelper.Link(,  { page = page - , pageSize = pageSize }) :  nextLink = page < totalPages -  ? urlHelper.Link(,  { page = page + , pageSize = pageSize }) :  paginationHeader = ==== results =*=>

Here we have added a Header that contains a Json serialized page metadata and the response received by the client:

The two paging technologies have their own advantages and disadvantages. I feel that I have made another choice when I encounter specific applications. The next chapter introduces Web Api security and implements Basic authentication.

Source Code address: https://github.com/fzrain/WebApi.eLearning

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.