ASP.NET Core Interview Question #25

What is the difference between Map, Use, and Run in the middleware pipeline?

ASP.NET Core Fundamentals Junior Beginner

Quick Interview Answer

Use adds middleware that can perform work and optionally call the next component in the pipeline. Run adds terminal middleware that does not call a next component. Map branches the pipeline based on a request path prefix.

Detailed Explanation

Use adds middleware that can perform work and optionally call the next component in the pipeline. Run adds terminal middleware that does not call a next component. Map branches the pipeline based on a request path prefix.

These methods are useful for understanding how the request pipeline is composed. In modern ASP.NET Core, endpoint mapping methods such as MapControllers and MapGet are also widely used to register executable endpoints.

Code Example

app.Use(async (context, next) =>
{
    await next();
});

app.Map("/admin", adminApp =>
{
    adminApp.Run(async context =>
        await context.Response.WriteAsync("Admin branch"));
});