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

Source: Internet
Author: User
Tags to domain

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

In Web APIs, CRUD operations on resources are implemented through the corresponding Http methods-Post (ADD), Put (modify), Delete (Delete), Get (query ). Query has been implemented in the previous chapters. This chapter implements the put, post, and delete methods in our case column (CourseController.

First, create the Post (CourseModel courseModel) method in "CourseController". The specific code is as follows:

 entity = (entity == ) Request.CreateErrorResponse(HttpStatusCode.BadRequest,  (TheRepository.Insert(entity) && Request.CreateErrorResponse(HttpStatusCode.BadRequest, 

The above code mainly involves the following:

1. The method name is Post. Therefore, the Http request initiated by the client must also be Post.

2. the method accepts a CourseModel parameter. For complex parameter Web APIs, they are deserialized from the Http Request Body, therefore, the client must send an object parameter representing the CourseModel type (I think it is the property name of CourseModel corresponding to Key-Value pairs, and Value is the property Value)

3. for the new operation, when the operation is successful, we return 201 (Resource Creation), while returning the newly created resource is also necessary, this resource contains an Id automatically generated on the server.

4. We added a Parse method in ModelFactory to convert our CourseModel to domain model ("Course"). This method code will not be pasted and will be provided in the Code in this chapter.

OK, let's test: Send a Post request (Http: // localhost: {your_port}/api/courses/) The request section is shown in:

Create the Put (int Id CourseModel courseModel) method in "CourseController". The specific code is as follows:

 HttpResponseMessage Put( updatedCourse = (updatedCourse == ) Request.CreateErrorResponse(HttpStatusCode.BadRequest,  originalCourse = (originalCourse ==  || originalCourse.Id != Request.CreateResponse(HttpStatusCode.NotModified, = (TheRepository.Update(originalCourse, updatedCourse) &&

Explain the above Code:

1. The method name is Put. Therefore, the client needs to send a put request. However, if the Patch feature is applied to our method, both put and patch requests will execute this method. The difference between put and patch requests is that when we need to update all fields of CourseModel, we use "put" and "Patch" to update only some fields ", however, we do not need to distinguish the two in our put method.

2. In our put method, two parameters (Id and CourseModel) are required. The Id is included in the URL, and the CourseModel is included in the request Body.

3. If the update is successful, we return the 200 status code and the updated Course. If the update is unsuccessful, the system returns the 304 (Not Modified) message ).

OK, let's test: Send a Put request (Http: // localhost: {your_port}/api/courses/1003) The request section is shown in:

Create the Delete (int Id) method in CourseController. The Code is as follows:

 HttpResponseMessage Delete( course = (course ==  (course.Enrollments.Count >  Request.CreateResponse(HttpStatusCode.BadRequest,  (TheRepository.DeleteCourse(id) &&

Explain the above Code:

1. The method name is delete, so the corresponding http request should be delete.

2. The method receives a parameter Id, and the Id should be set in the URL, so the content of the request body is empty.

3. If the deletion is successful, we will return the 200 status code. If the deletion fails, 400 (BadRequest) will be returned, and the error content will also be returned to the client.

OK. Let's test: Send A Delete request (Http: // localhost: {your_port}/api/courses/1003) The request section is shown in:

StudentController is used to implement CRUD for Students and mainly involves the following functions:

1. Use the Get request to obtain information about all students.

2. send a Get request to obtain the information of a single student (Note: here we pass UserName as a parameter to the server, this method is based on Basic authentication, therefore, only the user name and password can be used to query the relevant information. We will focus on this when talking about Web Api security ).

3. Add a student through the Post request.

4. modify a student using Put or Patch requests.

5. Delete a student.

The detailed code in StudentController is not provided here. It is basically similar to the Code in CourseController. You can obtain it in the Code link provided at the end of this chapter. Here we will list the routing code added in WebApiConfig:

 { controller = , userName =

So far, we have finished introducing CRUD for the resource corresponding to the Http method. In the next chapter, we will introduce the association between resources.

This chapter code: http://yun.baidu.com/share/link? Required id = 4231221159 & 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.