We are going to create the following API:
URL Endpoint | #Description | #Request body | #Response body |
---|---|---|---|
GET /api/todoitems |
Get all to-do items | None | Array of to-do items |
GET /api/todoitems/{id} |
Get all to-do item byid |
None | To-do items |
POST /api/todoitems |
Add a new item | To-do item | To-do items |
!PUT /api/todoitems/{id} |
Edit an existing item | To-do item | None |
DELETE /api/todoitems/{id} |
Delet a to-do items | None | None |
namespace To_Do_HandsOnAPIS.Models
{
// The model for this app is a single TodoItem class.
public class ToDoItem
{
public long Id { get; set; }
public string? Name { get; set; } // allow null val
public bool IsComplete { get; set; }
}
}
-
Right-click the Controllers folder.
-
Select Add > New Scaffolded Item.
-
Select API Controller with actions, using Entity Framework, and then select Add.
-
In the Add API Controller with actions, using Entity Framework dialog:
- Select TodoItem (TodoApi.Models) in the Model class.
- Select TodoContext (TodoApi.Models) in the Data context class.
- Select Add.