Middleware with Anonymous Inline Delegate & Decompression Support
Understand how to create middleware using anonymous inline delegates in ASP.NET Core Razor Pages to handle specific routes and analyze endpoint matches. Learn to enable built-in request decompression middleware to efficiently process compressed HTTP request bodies, improving web application performance.
Implementing an anonymous inline delegate as middleware
A delegate can be specified as an inline anonymous method. We will register one that plugs into the pipeline after routing decisions for endpoints have been made. It will output which endpoint was chosen, as well as handling one specific route: /bonjour. If that route is matched, it will respond with plain text without calling any further into the pipeline to find a match:
Step 1: In the Northwind.Web project, in Program.cs, add statements before the call to UseHttpsRedirection to use an anonymous method as a middleware delegate, as shown in the following code:
Step 2: Start the website.
Step 3: In the browser, navigate to https://localhost:5001, look at the console output and note a match on an endpoint route /; it was processed as /index, and the Index.cshtml Razor Page was executed to return the response, as shown in the ...