What is the difference between Run() and Use() methods in IApplicationBuilder?

  • Use() method: Used to create a simple middleware which can be "chained" to other functions over the pipeline. Takes two arguments: RequestDelegate and HttpContext. It is chained by calling the next() method on the RequestDelegate.
  • Run() method: "short-circuit" the request pipeline for a given condition. Takes a single argument of HttpContext and can be used to write on the Response.
app.Use(async (context, next) =>
{
    // Do something here
    // Call the next middleware
    await next.Invoke();
});

app.Run(async context =>
{
      // short-circuit the request 
      // write a response back
      await context.Response.WriteAsync("Hello from Run");
});
  • Use() method: Used to create a simple middleware which can be "chained" to other functions over the pipeline. Takes two arguments: RequestDelegate and HttpContext. It is chained by calling the next() method on the RequestDelegate.
  • Run() method: "short-circuit" the request pipeline for a given condition. Takes a single argument of HttpContext and can be used to write on the Response.
app.Use(async (context, next) =>
{
    // Do something here
    // Call the next middleware
    await next.Invoke();
});

app.Run(async context =>
{
      // short-circuit the request 
      // write a response back
      await context.Response.WriteAsync("Hello from Run");
});
Default image
Sriram Mannava

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity.

Leave a Reply