In the previous post, we have handled different GET requests with the help of a DTO object. In this post, we are going to create POST PUT DELETE requests and by doing so we are going to complete the server part (.NET Core part) of this series.

Let’s get into it.

If you want to see all the basic instructions and complete navigation for this series, please follow this link: Introduction page for this tutorial.

    The source code is available for download at .NET Core, Angular and MySQL. Part 6 – Source Code

    This post is divided into several sections:

    Let’s get right into it.

    Handling POST Request

    Firstly, let’s modify the decoration attribute for the action method GetOwnerById in the Owner controller:

    With this modification, we are setting the name for the action. This name will come in handy in the action method for creating a new owner.

    Before we continue, we should create another DTO class. As we said in the previous part, we use the model class just to fetch the data from the database, to return the result we need a DTO. It is the same for the create action. So, let’s create the OwnerForCreationDto class in the Entities/DataTransferObjects folder:

    As you can see, we don’t have the Id and Accounts properties.

    We are going to continue with the interface modification:

    After the interface modification, we are going to implement that interface:

    Before we modify the OwnerController, we have to create an additional mapping rule:

    CreateMap<OwnerForCreationDto, Owner>();

    Lastly, let’s modify the controller:

    Right now is a good time to test this code by sending the POST request by using Postman.

    Let’s examine the result:

    POST HTTP request in .NET Core

    Code Explanation

    Let’s talk a little bit about this code. The interface and the repository parts are pretty clear so we won’t talk about that. But the code in the controller contains several things worth mentioning.

    The CreateOwner method has its own [HttpPost] decoration attribute, which restricts it to the POST requests. Furthermore, notice the owner parameter which comes from the client. We are not collecting it from the Uri but from the request body. Thus the usage of the [FromBody] attribute. Also, the owner object is a complex type and because of that, we have to use [FromBody].

    If we wanted to, we could explicitly mark the action to take this parameter from the Uri by decorating it with the [FromUri] attribute, though I wouldn’t recommend that at all due to the security reasons and complexity of the request.

    Since the owner parameter comes from the client, it could happen that the client doesn’t send that parameter at all. As a result, we have to validate it against the reference type’s default value, which is null.

    Further down the code, you can notice this type of validation: if(!ModelState.IsValid). If you look at the owner model properties: Name, Address, and DateOfBirth, you will notice that all of them are decorated with the Validation Attributes. If for some reason validation fails, the ModelState.IsValid will return false as a result, signaling that something is wrong with the creation DTO object. Otherwise, it will return true which means that values in all the properties are valid.

    We have two map actions as well. The first one is from the OwnerForCreationDto type to the Owner type because we accept the OwnerForCreationDto object from the client and we have to use the Owner object for the create action. The second map action is from the Owner type to the OwnerDto type, which is a type we return as a result.

    The last thing to mention is this part of the code:

    CreatedAtRoute will return a status code 201, which stands for Created as explained in our post: The HTTP Reference. Also, it will populate the body of the response with the new owner object as well as the Location attribute within the response header with the address to retrieve that owner. We need to provide the name of the action, where we can retrieve the created entity:

    Post action header

    If we copy this address and paste it in Postman, once we send the GET request, we are going to get a newly created owner object.

    Handling PUT Request

    Excellent.

    Let’s continue with the PUT request, to update the owner entity.

    First, we are going to add an additional DTO class:

    We did the same thing as with the OwnerForCreationDto class. Even though this class looks the same as the OwnerForCreationDto, they are not the same. First of all, they have a semantical difference, this one is for update action and the previous one is for creation. Additionally, the validation rules that apply for the creation DTO doesn’t have to be the same for the update DTO. Therefore, it is always a good practice to separate those.

    One more thing, if you want to remove the code duplication from the OwnerForCreationDto and OwnerForUpdateDto, you can create an additional abstract class, extract properties to it and then just force these classes to inherit from the abstract class. Due to the sake of simplicity, we won’t do that now.

    After that, we have to create a new map rule:

    CreateMap<OwnerForUpdateDto, Owner>();

    Then, let’s change the interface:

    Of course, we have to modify the OwnerRepository.cs:

    Finally, alter the OwnerController:

    As you may have noticed, the action method is decorated with the [HttpPut] attribute. Furthermore, it receives two parameters: id of the entity we want to update and the entity with the updated fields, taken from the request body. The rest of the code is pretty simple. After the validation, we are pulling the owner from the database and executing the update of that owner. Finally, we return NoContent which stands for the status code 204:

    PUT HTTP request in .NET Core

    You can read more about Update actions in ASP.NET Core with EF Core to get a better picture of how things are done behind the scene. It could be very useful to upgrade quality of the update actions.

    Handling DELETE Request

    For the Delete request, we should just follow these steps:

    Interface:

    OwnerRepository:

    OwnerController:

    Let’s handle one more thing. If you try to delete the owner that has accounts, you are going to get 500 internal error because we didn’t allow cascade delete in our database configuration. What we want is to return a BadRequest. So, to do that let’s make a couple of modifications.

    Modify the IAccountRepository interface:

    Then modify the AccountRepository file by adding one new method:

    Finally, modify the DeleteOwner action in the OwnerController by adding one more validation before deleting the owner:

    So, that is it. Send the Delete request from Postman and see the result. The owner object should be deleted from the database.

    We have created these actions that use Repository Pattern logic synchronously but it could be done asynchronously as well. If you want to learn how to do that you can visit Implementing Async Repository in .NET Core. Although we strongly recommend finishing all the parts from this series for an easier understanding of the project’s business logic.

    Conclusion

    Now that you know all of this, try to repeat all the actions but for the Account entity. Because nothing beats the practice, doesn’t it? 😉

    With all this code in place, we have a working web API that covers all the features for handling the CRUD operations.

    By reading this post you’ve learned:

    • The way to handle the POST request
    • How to handle PUT request
    • How to write better and more reusable code
    • And the way to handle DELETE request

    Thank you for reading and I hope you found something useful in it.

    When you are ready, continue to Part 7 which is the part of the series where we introduce Angular. I will show you how to create an angular project and set up your first component.

    If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.