2.7 KiB
By Rick Anderson and Mike Wasson
HTTP is not just for serving up web pages. It’s also a powerful platform for building APIs that expose services and data. HTTP is flexible and ubiquitous. Almost any platform that you can think of has an HTTP library, so HTTP services can reach a broad range of clients, including browsers, mobile devices, and traditional desktop apps.
In this tutorial, you’ll build a web API for managing a list of "to-do" items. You won’t build a UI.
ASP.NET Core has built-in support for MVC creating Web APIs.
There are 3 versions of this tutorial:
- macOS: Web API with Visual Studio for Mac
- Windows: Web API with Visual Studio for Windows
- macOS, Linux, Windows: Web API with Visual Studio Code
Overview
Here is the API that you’ll create:
API | Description | Request body | Response body |
---|---|---|---|
GET /api/todo | Get all to-do items | None | Array of to-do items |
GET /api/todo/{id} | Get an item by ID | None | To-do item |
POST /api/todo | Add a new item | To-do item | To-do item |
PUT /api/todo/{id} | Update an existing item | To-do item | None |
DELETE /api/todo/{id} | Delete an item | None | None |
The following diagram shows the basic design of the app.
-
The client is whatever consumes the web API (mobile app, browser, etc). We aren’t writing a client in this tutorial. We'll use Postman or curl to test the app.
-
A model is an object that represents the data in your application. In this case, the only model is a to-do item. Models are represented as C# classes, also know as Plain Old C# Object (POCOs).
-
A controller is an object that handles HTTP requests and creates the HTTP response. This app will have a single controller.
-
To keep the tutorial simple, the app doesn’t use a persistent database. Instead, it stores to-do items in an in-memory database.