In the previous post, we have created a GET request with some code refactoring actions. 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 the following 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:


    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.

    Secondly, let’s modify the interface:

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

    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. You 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, you have to use [FromBody].

    If you wanted to, you 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.

    Because the owner parameter comes from the client, it could happen that the client doesn’t send that parameter at all. As a result, you would need to validate it against the reference type’s default value, which is null. Later in this post, we are going to refactor this line of code.

    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 model. Otherwise, it will return true which means that values in all the properties are valid.

    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. You need to provide the name of the action, where you can retrieve the created entity.

    Code Refactoring

    Everything is going great so far.


    Let’s make some refactoring which will help with the null validation of the CreateOwner method. Also, it will help with the validation of the GET actions.

    Just one tip: When you refactor your code, always try to do it in such a manner that will allow reusability as much as possible.

    Create a new interface inside Entities project:

    Then, we can modify the model classes: Owner, OwnerExtended, and Account, to implement that interface. In the same project, we are going to create the folder Extensions and in that folder create the new class IEntityExtensions:

    After all, that, let’s add an extension method for the IEntity type to validate against null value. Because the Owner and the Account inherit from the IEntity, we can extend any of those types and validate them.

    Now in the CreateOwner action method we are going to replace this code:

    with this one:

    Let’s refactor this code a little bit more.

    Create another extension method:

    Now let’s just change the code in the controller from the:

    to the:

    in the GetOwnerWithDetails and GetOwnerById actions.

    Handling PUT Request


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

    First, in the Entities project inside the Extensions folder we are going to create the OwnerExtensions class:

    And then, change the interface:

    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 you 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, you are pulling the owner from the database and executing the update of that owner. Finally, you return NoContent which stands for the status code 204.

    PUT HTTP request in .NET Core

    Handling DELETE Request

    For the Delete request, just follow these steps:




    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 I want is to return a BadRequest error and 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. 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 easier understanding of the project’s business logic.


    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 which covers all the features for handling the CRUD operations.

    By reading this post you’ve learned:

    • The way to handle 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 the 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.