app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello, World!"); }); });
public sealed class MyMiddleware { private readonly RequestDelegate next; public MyMiddleware(RequestDelegate next) { this.next = next; } public async Task InvokeAsync(HttpContext context) { // Log request information Console.WriteLine($"Request path: {context.Request.Path}"); // Call the next middleware component await next(context); // Log response information Console.WriteLine($"Response status code: {context.Response.StatusCode}"); } } // In Configure method in Startup.cs: app.UseMiddlewareThis example adds a custom middleware component to the HTTP pipeline that logs incoming request information and outgoing response information to the console. The package library for IPipelines is Microsoft.AspNetCore.Http.Abstractions, which provides a set of types that are used to build web applications, including HttpContext, HttpRequest, HttpResponse, and middleware components.();