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 examples 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, see how they differ from a server- rendered application, and find out when to use them.

Section 5.2 starts by expanding on the minimal API applications you’ve already seen. You’ll explore some basic routing concepts and show how values can be extracted from the URL automatically. Then you’ll learn how to handle additional HTTP verbs such as POST and PUT, and explore 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 see how 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.

5.1 What is an HTTP API, and when should you use one?

5.2 Defining minimal API endpoints

5.2.1 Extracting values from the URL with routing

5.2.2 Mapping verbs to endpoints

5.2.3 Defining route handlers with functions

5.3 Generating responses with IResult

5.3.1 Returning status codes with Results and TypedResults

5.3.2 Returning useful errors with Problem Details

5.3.3 Converting all your responses to Problem Details

5.3.4 Returning other data types

5.4 Running common code with endpoint filters

5.4.3 Generalizing your endpoint filters

sitemap