4 Handling requests with the middleware pipeline

 

This chapter covers

  • Understanding middleware
  • Serving static files using middleware
  • Adding functionality using middleware
  • Combining middleware to form a pipeline
  • Handling exceptions and errors with middleware

In chapter 3 you had a whistle-stop tour of a complete ASP.NET Core application to see how the components come together to create a web application. In this chapter, we’ll focus on one small subsection: the middleware pipeline.

In ASP.NET Core, middleware consists of C# classes or functions that handle an HTTP request or response. Middleware is chained together, with the output of one acting as the input to the next to form a pipeline.

The middleware pipeline is one of the most important parts of configuration for defining how your application behaves and how it responds to requests. Understanding how to build and compose middleware is key to adding functionality to your applications.

In this chapter you’ll learn what middleware is and how to use it to create a pipeline. You’ll see how you can chain multiple middleware components together, with each component adding a discrete piece of functionality. The examples in this chapter are limited to using existing middleware components, showing how to arrange them in the correct way for your application. In chapter 31 you’ll learn how to build your own middleware components and incorporate them into the pipeline.

4.1 Defining middleware

4.2 Combining middleware in a pipeline

4.2.1 Simple pipeline scenario 1: A holding page

4.2.2 Simple pipeline scenario 2: Handling static files

4.2.3 Simple pipeline scenario 3: A minimal API application

4.3 Handling errors using middleware

4.3.1 Viewing exceptions in development: DeveloperExceptionPage

4.3.2 Handling exceptions in production: ExceptionHandlerMiddleware

Summary

sitemap