Explore a comprehensive collection of tutorials designed to guide you through building robust and scalable web applications with the .NET ecosystem.
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
public class CustomMiddleware
{
private readonly RequestDelegate _next;
public CustomMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
// Log request details
context.Response.Headers.Add("X-Custom-Header", "HelloFromMiddleware");
await _next(context); // Call the next middleware in the pipeline
// Modify response if needed
// context.Response.StatusCode = 200;
}
}
// In Startup.cs or Program.cs:
// app.UseMiddleware<CustomMiddleware>();