liveBook · Manning

Chapter 4: Handling requests with the middleware pipeline

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

This chapter introduction frames middleware as the chained request-processing pipeline used to add application behavior, including cross-cutting concerns and error handling.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction frames middleware as the chained request-processing pipeline used to add application behavior, including cross-cutting concerns and error handling.

Key terms: middleware, middleware pipeline, HTTP request, HTTP response, cross-cutting concern, logging, error handling, security, holding page, static files

4.1 Defining middleware

This section defines ASP.NET Core middleware, explains how middleware components form a bidirectional request-response pipeline, and introduces HttpContext as the per-request object they share.

Key terms: middleware, HTTP request, HTTP response, EndpointMiddleware, pipeline, middleware pipeline, request, response, short-circuit, terminal middleware

4.2 Combining middleware in a pipeline

This section explains how focused middleware components are composed into a pipeline that processes requests and responses.

Key terms: middleware component, logging middleware, authentication middleware, static-file middleware, middleware pipeline, HttpContext, request, response, holding page, static files

4.2.1 Simple pipeline scenario 1: A holding page

This section builds a one-component Welcome Page middleware pipeline to show how ASP.NET Core receives every request and returns a simple holding-page response.

Key terms: WelcomePageMiddleware, holding page, Welcome-page response, ASP.NET Core web server, middleware pipeline, reverse proxy, HTML response, Program.cs, WebApplication, IApplicationBuilder

4.2.2 Simple pipeline scenario 2: Handling static files

This section shows how a single StaticFileMiddleware component serves files from wwwroot and how unhandled requests ultimately receive a 404 response.

Key terms: StaticFileMiddleware, UseStaticFiles, wwwroot, static files, middleware pipeline, 404 response, dummy middleware, HTTP response, status code, reason phrase

4.2.3 Simple pipeline scenario 3: A minimal API application

This section builds a realistic minimal API middleware pipeline and shows how middleware order, automatic additions, and short-circuiting determine which component handles a request or response.

Key terms: middleware pipeline, StaticFileMiddleware, RoutingMiddleware, EndpointMiddleware, MapGet, Use*(), middleware ordering, short-circuit, WelcomePageMiddleware, WebApplication

4.3 Handling errors using middleware

This section introduces why ASP.NET Core applications must explicitly add error-handling middleware and how exceptions and error status codes affect responses.

Key terms: error-handling middleware, opt-in, middleware pipeline, exception, NullReferenceException, 500 status code, 404 error, API, Razor Pages

4.3.1 Viewing exceptions in development: DeveloperExceptionPage

This section explains how DeveloperExceptionPageMiddleware exposes detailed exception information for debugging in development and why it must not be used in production.

Key terms: DeveloperExceptionPageMiddleware, developer exception page, middleware pipeline, exception, 500 status code, stack trace, Development environment, production, WebApplication, security risk

4.3.2 Handling exceptions in production: ExceptionHandlerMiddleware

This section explains how ExceptionHandlerMiddleware safely handles unhandled production errors by reexecuting the pipeline to produce a custom error response.

Key terms: ExceptionHandlerMiddleware, developer exception page, Development environment, error-handling path, hosting environment, reexecute the pipeline, partial response, 500 error, Razor Pages, reexecuting the pipeline

4.4 Summary

This summary recaps how the ordered, two-way middleware pipeline processes requests and responses, handles static files and errors, and can stop or reexecute processing.

Where this is used later

Concepts explained in this chapter

← Chapter 3: Your first application Chapter 5: Creating a JSON API with minimal APIs →

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.