liveBook · Manning

Chapter 20: Creating an HTTP API using web API controllers

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

This chapter introduces web API controllers as an ASP.NET Core approach for returning client-friendly API responses, building on familiar MVC, routing, binding, and validation concepts.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces web API controllers as an ASP.NET Core approach for returning client-friendly API responses, building on familiar MVC, routing, binding, and validation concepts.

Key terms: web API controllers, minimal APIs, JSON, SPAs, MVC controllers, routing, MVC design pattern, model binding, validation, action methods

20.1 Creating your first web API project

This section creates a web API controller project and shows how controller actions route requests and return data or HTTP results.

Key terms: ASP.NET Core Web API template, dotnet new webapi, AddControllers(), MapControllers(), OpenAPI, Swagger UI, web API controller, ControllerBase, Controller, [ApiController]

20.2 Applying the MVC design pattern to a web API

This section shows that web API controllers follow the same MVC-style request flow as Razor Pages, differing chiefly in how request and response data are represented and formatted.

Key terms: RoutingMiddleware, EndpointMiddleware, model binding, validation, ModelState, ControllerBase, application model, services, business logic, page handlers

20.3 Attribute routing: Linking action methods to URLs

This section introduces attribute routing for web API controllers, showing how route attributes connect action methods to URL templates and reuse standard ASP.NET Core route-template features.

Key terms: attribute routing, [Route] attribute, route template, action method, explicit routing, RouteAttribute, controller, route parameter, literal segment, optional parameter

20.3.1 Combining route attributes to keep your route templates DRY

This subsection shows how controller-level and action-level route attributes combine to remove repeated route prefixes while allowing an action to define an absolute route.

Key terms: RouteAttribute, controller, action method, route template, DRY, prefix, absolute route template, controller RouteAttribute

20.3.2 Using token replacement to reduce duplication in attribute routing

This section shows how [controller] and [action] token replacement reduces repeated attribute-route text while warning that renames can change public API URLs.

Key terms: [controller], [action], token replacement, attribute routes, RouteAttributes, public API, static strings

20.3.3 Handling HTTP verbs with attribute routing

This section explains that web API controller routing matches both a URL template and an HTTP verb, using verb-specific attributes to select actions and returning 405 for unsupported methods.

Key terms: HTTP verb, routing, web API controllers, Razor Pages, RoutingMiddleware, action method, [Route], [HttpGet], [HttpPost], [HttpPut]

20.4 Using common conventions with [ApiController]

This section shows how [ApiController] applies web API conventions that remove repetitive controller code and how those conventions can be customized.

Key terms: [ApiController], attribute routing, automatic 400 responses, model binding source inference, ProblemDetails, ModelState, [FromBody], ModelState.IsValid, ValidationProblemDetails, 400 Bad Request

20.5 Generating a response from a model

This section introduces how web API controllers format returned models through content negotiation, response headers, and output formatters.

Key terms: content negotiation, conneg, Accept header, Content-Type header, MIME types, weightings, q value, IOutputFormatter, JSON, XML

20.5.1 Customizing the default formatters: Adding XML support

This section shows how to customize web API formatters by switching JSON libraries and adding XML support so controller responses can honor a client's requested format.

Key terms: formatters, text/plain, text/html, application/json, XML output formatter, System.Text.Json, Newtonsoft.Json, Json.NET, Microsoft.AspNetCore.Mvc.NewtonsoftJson, AddControllers()

20.5.2 Choosing a response format with content negotiation

This section explains how ASP.NET Core web API controllers choose a response format through content negotiation and the default cases that can alter that choice.

Key terms: content negotiation, Accept header, IOutputFormatters, MIME types, application/json, text/plain, text/html, 204 No Content, StatusCodeResult, */*

20.6 Choosing between web API controllers and minimal APIs

This section provides criteria for choosing minimal APIs or web API controllers for an HTTP API, while emphasizing that the final choice depends on an application's needs and team preferences.

Key terms: content negotiation, conneg, minimal APIs, web API controllers, filter pipeline, MVC framework, consistency, convention over configuration, action methods, controller class

20.7 Summary

This summary recaps how web API controllers route requests, apply API conventions, format responses, and compare with minimal APIs.

Where this is used later

Concepts explained in this chapter

← Chapter 19: Creating a website with MVC controllers Chapter 21: The MVC and Razor Pages filter pipeline →

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.