Skip to content
Learn Netverks

Lesson

Step 16/36 44% through track

middleware-pipeline

Middleware pipeline

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Middleware pipeline: the syntax, APIs, and habits you need before advancing in ASP.NET.

The middleware pipeline is ASP.NET Core’s request spine—ordering and scoped services trip up new hires constantly.

You will apply Middleware pipeline in contexts like: Line-of-business APIs, intranets, BFF layers, and cloud-hosted services on Linux or Windows.

Write C# (top-level or Program class), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Each HTTP request passes through a chain of middleware components—authentication, routing, static files, exception handling—before reaching your endpoint. Order matters: first registered runs first on the way in, last on the way out.

Typical pipeline order

  1. Exception handling / HSTS (production)
  2. HTTPS redirection
  3. Static files
  4. Routing
  5. Authentication
  6. Authorization
  7. Endpoints (controllers, minimal APIs)
app.UseExceptionHandler("/Error");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

Custom middleware sketch

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

Important interview questions and answers

  1. Q: Middleware vs filter?
    A: Middleware sees all requests globally; action filters run around MVC actions only.
  2. Q: Why UseAuthentication before UseAuthorization?
    A: Authorization needs an authenticated user identity established first.

Self-check

  1. What runs first: routing or static files?
  2. Where does exception middleware usually sit?

Tip: Draw the pipeline on paper: request enters top middleware, exits bottom. UseAuthentication must precede UseAuthorization—order is dependency order, not alphabetical.

Interview prep

Middleware vs MVC filter?

Middleware runs for every request globally; action filters wrap MVC actions only—use middleware for cross-cutting HTTP concerns like exception handling.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Order matters example?
  • UseMiddleware when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump