In the previous post, we have created a GET request with some code refactoring actions. In this post, we are going to create a POST, PUT and 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.

For the previous part check out: Part 5 – Creating .NET Core WebApi project – Using repository for the GET requests

The source code is available for download at .NET Core, Angular 4 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 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 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 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:




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


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 series where we introduce Angular. I will show you how to create the angular project and set up your first component.

For any suggestions or questions, don’t hesitate to leave the comment in the comment section below.