Use ASP. NET Web Api to build a REST-based service practice series [6] & mdash; & mdash

Source: Internet
Author: User

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

This article mainly introduces the association between resources-for example, there is such association between students and courses: each course will be selected by multiple students, how do I obtain the associated information? How to Implement the course selection business? How can I call a client? The solution is as follows:

For the above requirements, we can first create a URI template: "api/courses/courseid/students/{userName}". If we want to query all the students whose courseId is 5, then the client should send the Get request "api/courses/5/students /". if we want to select a course with the course Id 5 for students whose username is "TaiseerJoudeh", we should send a Post request "api/courses/5/students/TaiseerJoudeh".

To implement the above, we first need to add the following route information in "WebApiConfig:

 { controller = , userName =

Here we can see that courseId is mandatory, and userName is optional.

As shown in the preceding routing configuration, we should have an "EnrollmentsController". In this controller, we should include two methods: Get to return the corresponding student information, and Post to implement student elective, the code below:

  IEnumerable<StudentBaseModel> Get(<Student>= TheRepository.GetEnrolledStudentsInCourse(courseId).OrderBy(s => totalCount = results ==> HttpResponseMessage Post( courseId, [FromUri] (!TheRepository.CourseExists(courseId))  Request.CreateErrorResponse(HttpStatusCode.BadRequest,  student = (student == )  Request.CreateErrorResponse(HttpStatusCode.BadRequest,  result = (result ==   (result ==  Request.CreateResponse(HttpStatusCode.NotModified, 

In the Post method, we use the FormURL and FormBody features, which indicates that we will obtain parameters from the request (get UserName from URL and get enrollment from Body ), for an enrollment object, you only need to include a date (for more information, see the build of the series database and model: http://www.cnblogs.com/fzrain/p/3491804.html), and we will also send "courseId" and "UserName ".

Next we will test our Post method: We plan to select a course with CourseId 5 for "TaiseerJoudeh:

The association between resources is so far, and the specific application will be realized later. There is an obvious flaw in the previous Get method: for a large amount of data, the server will return all the data to the client, which is very bad. In the next chapter, we will implement paging.

This chapter source code: http://yun.baidu.com/share/link? Required id = 1470307710 & uk = 17559114 & third = 0

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.