liveBook · Manning

Chapter 5: Creating a JSON API with minimal APIs

What you will learn in chapter 5 of ASP.NET Core in Action, 3e

This chapter introduces building full-featured JSON HTTP APIs with minimal APIs, including endpoint definition, responses, reusable filters, and route groups.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces building full-featured JSON HTTP APIs with minimal APIs, including endpoint definition, responses, reusable filters, and route groups.

Key terms: HTTP API, minimal API, JSON, server-rendered application, minimal APIs, routing, URL, POST, PUT, route handlers

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

This section defines HTTP APIs, shows how SPAs, mobile apps, and services consume them, and explains when an API backend is preferable to server-rendered HTML.

Key terms: HTTP API, JSON API, REST API, endpoint, JSON, single-page application, SPA, client-side application, Blazor WebAssembly, client-side SPA

5.2 Defining minimal API endpoints

Introduces the upcoming exploration of minimal API endpoints that handle multiple HTTP verbs and use different endpoint-handler styles.

Key terms: minimal API endpoints, HTTP verbs, endpoint handlers

5.2.1 Extracting values from the URL with routing

This subsection shows how parameterized minimal API routes extract a value from a request URL path and provide it to an endpoint lambda.

Key terms: parameterized routes, route template, name parameter, lambda function endpoint, request URL’s path, fixed paths, dynamic APIs

5.2.2 Mapping verbs to endpoints

This section explains how minimal API Map* methods associate HTTP verbs with endpoints, why verb semantics matter, and how incorrect verbs are handled and tested.

Key terms: HTTP verb, GET, POST, DELETE, RESTful applications, semantics, MapGet(), MapPost(), MapMethods(), Map()

5.2.3 Defining route handlers with functions

This section shows that minimal API endpoints can use several equivalent handler forms, automatically bind one JSON body object, and should be organized modularly in real applications.

Key terms: route handler, lambda expression, Func<T, TResult>, static method, instance method, local function, complex types, JSON body, deserializing, System.Text.Json

5.3 Generating responses with IResult

This section introduces IResult as the minimal API return type for producing deliberate HTTP responses, especially error status codes, and contrasts it with other handler return types.

Key terms: IResult, ExecuteAsync, 500 Internal Server Error, 404 Not Found, 400 Bad Request, void, Task, Task<IResult>, POCO, text/plain

5.3.1 Returning status codes with Results and TypedResults

This section shows how minimal API endpoints return meaningful HTTP status-code responses using Results, TypedResults, or, less commonly, HttpResponse directly.

Key terms: 200 OK, 201 Created, 204 No Content, 400 Bad Request, 404 Not Found, Location header, Results, TypedResults, IResult, Ok<T>

5.3.2 Returning useful errors with Problem Details

This section introduces Problem Details as the standard JSON format for consistent API errors and shows how minimal APIs generate it.

Key terms: Problem Details, machine-readable errors, HTTP APIs, validation errors, Results.Problem(), Results.ValidationProblem(), TypedResults, ProblemHttpResult, Dictionary of validation errors, Problem Details JSON

5.3.3 Converting all your responses to Problem Details

This section shows how to make minimal API error responses consistently use Problem Details JSON for both exceptions and error status codes.

Key terms: Problem Details, exceptions, error status code responses, AddProblemDetails(), IProblemDetailsService, UseExceptionHandler(), ExceptionHandlerMiddleware, DeveloperExceptionPageMiddleware, error-handling path, HTML developer exception page

5.3.4 Returning other data types

This section shows how minimal API endpoints return files, binary data, and streams, and when to create responses manually or with a custom IResult.

Key terms: Results.File(), Results.Byte(), Results.Stream(), content type, filename, range requests, Range header, enableRangeProcessing, IResult, custom IResult

5.4 Running common code with endpoint filters

This section introduces endpoint filters as a way to move cross-cutting code such as validation out of minimal API handlers and run it before or after an endpoint.

Key terms: endpoint filters, cross-cutting activities, endpoint handler, validation, logging, authorization, AddEndpointFilter(), endpoint filter pipeline, EndpointFilterInvocationContext, next

5.4.1 Adding multiple filters to an endpoint

This subsection explains how multiple endpoint filters form an ordered, nestable pipeline, can short-circuit later work, and should be used sparingly for genuinely repeated endpoint-specific concerns.

Key terms: endpoint filter pipeline, endpoint handler, AddEndpointFilter, next, short-circuit, next(context), Problem Details, result, cross-cutting concerns, middleware

5.4.2 Filters or middleware: Which should you choose?

This section compares endpoint filters with middleware and gives a specificity-based rule for choosing between them.

Key terms: middleware pipeline, endpoint filter pipeline, short-circuit, cross-cutting concerns, HttpContext, EndpointMiddleware, IResult, middleware, endpoint filters, exception-handling middleware

5.4.3 Generalizing your endpoint filters

This section shows how endpoint filter factories create endpoint-specific filters that avoid assumptions about a handler’s parameter order.

Key terms: AddEndpointFilterFactory(), filter factory, filter function, filter pipeline, EndpointFilterFactoryContext, MethodInfo, EndpointMetadata, EndpointFilterInvocationContext, id parameter, next

5.4.4 Implementing the IEndpointFilter interface

This section shows how to implement a minimal API endpoint filter as an IEndpointFilter class and when that approach is preferable to lambda-based filters.

Key terms: IEndpointFilter, InvokeAsync(), AddEndpointFilter(), IntelliSense, autocompletion, filter-factory pattern, lambda, helper method

5.5 Organizing your APIs with route groups

This section shows how route groups reduce duplicated route prefixes and endpoint filters when organizing minimal API endpoints.

Key terms: MapGroup(), WebApplication, RouteGroupBuilder, route prefix, Map* extension methods, AddEndpointFilter(), endpoint filter, nested groups, prefix

5.6 Summary

This summary recaps how to design minimal HTTP API endpoints, generate appropriate responses, and organize shared endpoint behavior.

Where this is used later

Concepts explained in this chapter

← Chapter 4: Handling requests with the middleware pipeline Chapter 6: Mapping URLs to endpoints using routing →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.