ASP.NET Core Interview Question #79
What is the difference between Use, Run, and Map in ASP.NET Core?
Middleware & Request Pipeline Mid-Level Intermediate
Quick Interview Answer
Use adds middleware that can call the next component in the pipeline. It is appropriate when processing should continue after the middleware performs its work.
Detailed Explanation
Use adds middleware that can call the next component in the pipeline. It is appropriate when processing should continue after the middleware performs its work.
Run adds terminal middleware. It handles the request and does not call another middleware component.
Map branches the pipeline based on the request path. Requests matching the specified path enter the mapped branch, while other requests continue through the main pipeline.
Choosing the correct method makes pipeline flow explicit and avoids accidental short-circuiting.
Code Example
app.Use(async (context, next) =>
{
await next();
});
app.Map("/health", healthApp =>
{
healthApp.Run(async context =>
await context.Response.WriteAsync("Healthy"));
});
app.Run(async context =>
{
await context.Response.WriteAsync("Fallback response");
});