19 Building custom components

 

This chapter covers

  • Building custom middleware
  • Creating simple endpoints that generate a response using middleware
  • Using configuration values to set up other configuration providers
  • Replacing the built-in DI container with a third-party container

When you’re building apps with ASP.NET Core, most of your creativity and specialization goes into the services and models that make up your business logic and the Razor Pages and controllers that expose them through views or APIs. Eventually, however, you’re likely to find that you can’t quite achieve a desired feature using the components that come out of the box. At that point, you may need to build a custom component.

This chapter shows how you can create some ASP.NET Core components that you’re likely to need as your app grows. You probably won’t need to use all of them, but each solves a specific problem you may run into.

We’ll start by looking at the middleware pipeline. You saw how to build pipelines by piecing together existing middleware in chapter 3, but in this chapter you’ll create your own custom middleware. You’ll explore the basic middleware constructs of the Map, Use, and Run methods and learn how to create standalone middleware classes. You’ll use these to build middleware components that can add headers to all your responses as well as middleware that returns responses.

19.1 Customizing your middleware pipeline

19.1.1 Creating simple endpoints with the Run extension

19.1.2 Branching middleware pipelines with the Map extension

19.1.3 Adding to the pipeline with the Use extension

19.1.4 Building a custom middleware component

19.2 Creating custom endpoints with endpoint routing

19.2.1 Creating a custom endpoint routing component

19.2.2 Creating simple endpoints with MapGet and WriteJsonAsync

19.2.3 Applying authorization to endpoints

19.3 Handling complex configuration requirements

19.3.1 Partially building configuration to configure additional providers

19.3.2 Using services to configure IOptions with IConfigureOptions

19.4 Using a third-party dependency injection container

Summary