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:
A browser, mobile app, or API client sends an HTTP request:
-
URL (e.g.,
/api/products) - Method (GET, POST, etc.)
- Headers, body
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
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();
Request → Middleware 1 → Middleware 2 → Middleware 3 → Endpoint
← Response flows back
- Exception handling
- Static files
- Routing
- Authentication & Authorization
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)
Once matched, the request reaches:
- Controller class handles request
- Action method executes logic
app.MapGet("/hello", () => "Hello World");
Before the action runs:
- Data from request (JSON, query, route) is converted into C# objects
- Validation attributes are applied
Inside the action:
- Calls services (via Dependency Injection)
- Interacts with database, APIs, etc.
The action returns:
-
JSON (
Ok(object)) - View (MVC)
- Status codes
The response goes back through the same middleware in reverse order:
- Logging
- Error handling
- Response modification
Finally:
- Kestrel sends the HTTP response back to the client
Client → Kestrel → Middleware → Routing → Endpoint → Business Logic
←──────────── Response (back through middleware) ───────────
- Middleware = pipeline backbone
- Routing decides where to go
- Controllers/Endpoints handle logic
- Dependency Injection is used everywhere