December 2019
Intermediate to advanced
510 pages
11h 33m
English
Middleware can also be implemented by using a class-based approach. This kind of approach increases the reusability, testability, and maintainability of middleware. A class-based approach involves the definition of a new type, for example. Let's have a look at class-based middleware:
using System.Threading.Tasks;using Microsoft.AspNetCore.Http;namespace Demo.WebAPI{ public class SampleMiddleware { private readonly RequestDelegate _next; public RequestCultureMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context) { //DO STUFF // Call the next delegate/middleware in the pipeline await _next(context); } }}
Let's examine some key points in this class: