ASP.NET Core Interview Question #82

What is RequestDelegate?

Middleware & Request Pipeline Mid-Level Intermediate

Quick Interview Answer

RequestDelegate represents a function that processes an HttpContext asynchronously. It is the fundamental delegate used to connect middleware components in the ASP.NET Core request pipeline.

Detailed Explanation

RequestDelegate represents a function that processes an HttpContext asynchronously. It is the fundamental delegate used to connect middleware components in the ASP.NET Core request pipeline.

A conventional middleware receives the next RequestDelegate in its constructor. Calling that delegate passes the current HttpContext to the next component.

If the middleware does not call the next delegate, it becomes terminal for that request.

Code Example

public delegate Task RequestDelegate(HttpContext context);

public class SampleMiddleware
{
    private readonly RequestDelegate _next;

    public SampleMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public Task InvokeAsync(HttpContext context)
        => _next(context);
}