Every project has its own business or domain-specific requirements for the request pipeline processing. The built-in middleware is good enough, but writing custom middleware gives us more control over the request processing strategy.
In this section, we will write the custom middleware component with respect to web API projects. A custom middleware can be written by considering these points:
- A C# class with a middleware name
- A RequestDelegate variable--_next--to invoke the next middleware in the pipeline
- The class constructor taking in a request delegate object that is injected using DI
- An Invoke method accepting HttpContext as parameter and returning Task asynchronously
We will write a custom middleware with a business scenario, as every request should contain the packt-book header, whose value should be Mastering Web API. If the header value is missing...