5 Creating a JSON API with minimal APIs
This chapter covers
- Creating a minimal API application to return JSON to clients
- Generating responses with
IResult
- Using filters to perform common actions like validation
- Organizing your APIs with route groups
So far in this book you’ve seen several examples of minimal API applications that return simple Hello World responses. These are great for getting started, but you can also use minimal APIs to build full-featured HTTP API applications. In this chapter you’ll learn about HTTP APIs, how they differ from a server-rendered application, and when to use them.
In section 5.2 you’ll start by expanding on the minimal API applications you’ve already seen. We’ll explore some basic routing concepts and show how values can be automatically extracted from the URL. You’ll then learn how to handle additional HTTP verbs like POST
and PUT
, and explore the various ways to define your APIs.
In section 5.3 you’ll learn about the different return types you can use with minimal APIs. You’ll learn to use the Results
and TypedResults
helper classes to easily create HTTP responses that use status codes like 201 Created
and 404 Not Found
. You’ll also learn how to follow web standards for describing your errors by using the built-in support for Problem Details.