Tool for HR, Hiring Managers, and the Leadership Team

Difference between Use, Run, Map?

In .NET Core Middleware pipeline, Use, Run, and Map control how request flows through middleware.

Think of them like traffic controllers 🚦

1. Use() — Continue to Next Middleware

Use() can call next middleware in pipeline.

Example

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

    await next();

    Console.WriteLine("Middleware 1 - After");
});

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

    await next();
});

Output

Middleware 1 - Before
Middleware 2
Middleware 1 - After

Key Points

✅ Can call next middleware
✅ Can modify request/response
✅ Most commonly used

2. Run() — Terminates Pipeline

Run() does NOT call next middleware
It ends the pipeline immediately 🛑

Example

app.Use(async (context, next) =>
{
    Console.WriteLine("Middleware 1");
    await next();
});

app.Run(async context =>
{
    Console.WriteLine("Middleware 2 - Run");
});

Output

Middleware 1
Middleware 2 - Run

If you add middleware after Run():

app.Run(async context =>
{
    Console.WriteLine("Run Middleware");
});

app.Use(async (context, next) =>
{
    Console.WriteLine("This will never execute");
});

❌ This middleware will never execute

3. Map() — Branch the Pipeline

Map() creates a separate pipeline based on URL path 🔀

Example

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

app.Run(async context =>
{
    await context.Response.WriteAsync("Home Page");
});

Results

/admin  → Admin Page
/home   → Home Page

Visual Comparison

Use → Continue pipeline
Run → End pipeline
Map → Branch pipeline

Interview Table (Very Important) 

Feature Use Run Map
Calls next middleware ✅ Yes ❌ No Depends
Ends pipeline ❌ No ✅ Yes Branch
Creates branch ❌ No ❌ No ✅ Yes
Most common ✅ Yes ❌ No ⚠️ Sometimes

Interview Answer (Short Version)

Use() continues to next middleware.
Run() terminates the pipeline.
Map() creates branch pipeline based on URL path.

Real-world Example

app.UseAuthentication();
app.UseAuthorization();

app.Map("/admin", admin =>
{
    admin.UseAuthorization();
});

app.Run();

Trick Interview Question 

Q: Can we place Run() in middle?
👉 Yes, but everything after that won't execute

If you'd like, I can also explain:

  • UseWhen vs MapWhen (Very common interview question)

  • Middleware execution order

  • Real-world production middleware examples