What is RequestDelegate in .NET Core?

Asked 8 hours ago Updated 7 hours ago 30 views

1 Answer


1

In simple terms, RequestDelegate in .NET Core is a function that handles an incoming HTTP request.

Think of it like this:

  • A user opens your website.
  • The request enters your ASP.NET Core application.
  • RequestDelegate receives that request and decides what to do with it.

It can either:

  • Generate a response and send it back to the user, or
  • Pass the request to the next middleware in the pipeline.

Real-life analogy

Imagine you're at a restaurant.

  • A customer = HTTP request
  • A waiter = RequestDelegate
  • The waiter either:
  • Serves the customer directly, or
  • Takes the order to the next person (chef, cashier, etc.).

Similarly, a RequestDelegate either handles the request itself or forwards it to the next middleware.

What is RequestDelegate in .NET Core?

Simple example

app.Run(async context =>
{
    await context.Response.WriteAsync("Hello World!");
});

Here, app.Run() creates a RequestDelegate.

When someone visits the website:

  • The request comes in.
  • The RequestDelegate runs.
  • It writes "Hello World!" to the response.
  • The request ends.

Another example with middleware

app.Use(async (context, next) =>
{
    Console.WriteLine("Before");

    await next(); // Pass request to the next RequestDelegate

    Console.WriteLine("After");
});

Here:

  • "Before" runs first.
  • await next() sends the request to the next RequestDelegate.
  • After the next one finishes, "After" runs.

In one sentence

RequestDelegate is a method that processes an HTTP request and either returns a response or passes the request to the next middleware in the ASP.NET Core request pipeline.

Write Your Answer