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