Add CRUD operations in an ASP.NET Core Web API

Published: Wednesday 6 September 2023

ASP.NET Core Web APIs can be used to perform create, read, update and delete operations.

Known as CRUD, each one is assigned to a HTTP request method and can be used in an API controller.

Here is a list of the operations and the method that they belong to:

CRUD operation HTTP request method
Read GET
Create POST
Update PUT
Delete DELETE

In ASP.NET Core, we can use the [HttpGet], [HttpPost], [HttpPut] and [HttpDelete] attributes to determine what request method is used within a controller's action.

Go ahead and create a new Web API controller with an action for each HTTP request method.

Each action should:

  • Have an HTTP attribute assigned to it depending on the request method used. You don't need to include a name for this exercise
  • The method name can be anything you like
  • Return a type of IActionResult
  • Return the method Ok();

As a starter, we've created a MyApiController with an action using the GET request method.

[ApiController]
[Route("api/my")]
public class MyApiController : Controller
{
	[HttpGet]
	public IActionResult Read()
	{
		return Ok();
	}
}

With that same controller, add actions for the other three HTTP request methods.