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");

ASP.NET Core Posted Nov 08, 2020

You can now show your support. 😊

We use cookies to provide you with a great user experience, analyze traffic and serve targeted promotions.   Learn More   Accept