Sunday, April 5, 2026

Dotnet | Request Pipeline

A request in .NET Core (now commonly called ASP.NET Core) flows through a well-defined pipeline. Think of it as a chain of steps that process an HTTP request and produce a response.



Here’s the flow in a clear, conceptual way:


1. Client Sends HTTP Request

A browser, mobile app, or API client sends an HTTP request:

  • URL (e.g., /api/products)
  • Method (GET, POST, etc.)
  • Headers, body

2. Web Server (Kestrel)

The request first hits the built-in web server:

  • Kestrel
  • It listens for incoming HTTP requests and forwards them to the app

Optionally, Kestrel can sit behind:

  • IIS
  • Nginx

3. Middleware Pipeline (Core of the Flow)

This is the most important part.

Middleware are components executed in sequence. Each one can:

  • Handle the request
  • Modify it
  • Pass it to the next middleware

Typical pipeline setup (in Program.cs):

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
Flow inside middleware:
Request → Middleware 1 → Middleware 2 → Middleware 3 → Endpoint
← Response flows back
Common middleware:
  • Exception handling
  • Static files
  • Routing
  • Authentication & Authorization

4. Routing

Routing determines which endpoint should handle the request.

  • Matches URL pattern
  • Maps to a controller/action or minimal API

Example:

[HttpGet("products/{id}")]
public IActionResult GetProduct(int id)

5. Endpoint Execution

Once matched, the request reaches:

Option A: Controllers (MVC/Web API)
  • Controller class handles request
  • Action method executes logic
Option B: Minimal APIs
app.MapGet("/hello", () => "Hello World");

6. Model Binding & Validation

Before the action runs:

  • Data from request (JSON, query, route) is converted into C# objects
  • Validation attributes are applied

7. Business Logic & Services

Inside the action:

  • Calls services (via Dependency Injection)
  • Interacts with database, APIs, etc.

8. Response Creation

The action returns:

  • JSON (Ok(object))
  • View (MVC)
  • Status codes

9. Response Travels Back Through Middleware

The response goes back through the same middleware in reverse order:

  • Logging
  • Error handling
  • Response modification

10. Response Sent to Client

Finally:

  • Kestrel sends the HTTP response back to the client

Quick Summary (Flow)
Client → Kestrel → Middleware → Routing → Endpoint → Business Logic
←──────────── Response (back through middleware) ───────────

Key Concepts to Remember
  • Middleware = pipeline backbone
  • Routing decides where to go
  • Controllers/Endpoints handle logic
  • Dependency Injection is used everywhere

No comments:

Post a Comment

Node | Cluster Vs Worker Threads

Cluster: Multiple processes (scale app across CPU cores) Worker Threads: Multiple threads (handle CPU-heavy work inside one process) Cluster...