public class PrintSomethingMiddleware
{
private readonly RequestDelegate _next;
public PrintSomethingMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
// Print headers
foreach (var header in context.Request.Headers)
{
Console.WriteLine(header.Key + "-" + header.Value);
}
// Call the next delegate/middleware in the pipeline
await _next(context);
}
}
public static class IApplicationBuilderExtensions
{
public static IApplicationBuilder UsePrintSomething(this IApplicationBuilder builder)
{
return builder.UseMiddleware<PrintSomethingMiddleware>();
}
}
app.UsePrintSomething();