| Kestrel In .NET Core :👈 | 👉:API Error Handling Best Practices |
ASP.NET Core Request Pipeline |
ASP.NET Core Request Processing Pipeline
ASP.NET Core and walk through exactly how the request processing pipeline works internally. Think of it as an assembly line for HTTP requests, where each station (middleware) can inspect, modify, or short-circuit the process before handing it to the next.
This explains how an HTTP request flows from the browser through ASP.NET Core middlewares until a response is returned.
π§© High-Level Flow
1οΈβ£ Request Leaves the Browser
- A user types a URL (e.g.,
https://example.com/home/index) or clicks a link. - The browser creates an HTTP request and sends it over the internet.
2οΈβ£ Request Reaches the Server
- In production, a reverse proxy like IIS, Nginx, or Apache may receive the request first.
- The reverse proxy may handle SSL/TLS, load balancing, logging, then forward the request to Kestrel.
- In development, the request usually goes directly to Kestrel.
3οΈβ£ Kestrel (Web Server)
- Kestrel is the built-in ASP.NET Core web server.
- It accepts the HTTP request and passes it to the middleware pipeline.
4οΈβ£ Middleware Pipeline Execution
The middleware pipeline is defined in Program.cs or Startup.Configure. Each middleware can inspect, modify, or short-circuit the request.
Example sequence for a typical MVC/Web API app:
- Exception Handling β catches unhandled errors.
- HSTS / HTTPS Redirection β enforces secure HTTPS connections.
- Static Files β serves CSS, JS, and images directly.
- Routing β prepares request routing.
- Authentication β validates the user identity.
- Authorization β checks user permissions.
- Endpoint Execution β executes the controller, Razor Page, or Minimal API.
5οΈβ£ Your Application Logic
- The controller or endpoint runs business logic.
- It may query databases or call external services.
- A response (HTML, JSON, file, etc.) is generated.
6οΈβ£ Response Travels Back Out
- The response flows back through the middleware pipeline in reverse order.
- Middleware can add headers, compress content, or log responses.
- Kestrel sends the response to the client.
7οΈβ£ Browser Receives Response
- The browser renders HTML pages.
- Or processes JSON/data if it was an API call.
π Internal Mechanics
RequestDelegate Chain Internally, the pipeline is a chain of RequestDelegate functions. Each middleware receives:
Task InvokeAsync(HttpContext context, RequestDelegate next)
contextβ Holds request/response data.nextβ Calls the next middleware in the chain.
Short-Circuiting If a middleware doesnβt call await next(context), the pipeline stops there (e.g., app.Run(...)).
Onion Model The request goes inward through middleware layers, hits the endpoint, then comes outward through the same layers in reverse β like peeling an onion.
π Example Pipeline Setup
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseExceptionHandler("/error"); // 1. Global error handling
app.UseHttpsRedirection(); // 2. Redirect HTTP β HTTPS
app.UseStaticFiles(); // 3. Serve static files
app.UseRouting(); // 4. Enable routing
app.UseAuthentication(); // 5. Authenticate user
app.UseAuthorization(); // 6. Authorize user
app.UseEndpoints(endpoints => // 7. Map endpoints
{
endpoints.MapControllers();
});
}
π§ Key Things to Remember
- Order matters β middleware earlier in the pipeline can block or modify requests before they reach later components.
- Statelessness helps scaling β makes it easier to run multiple instances behind a load balancer.
- Middleware vs Filters β middleware runs for all requests; filters run inside MVC after routing.
π‘ Pro Tip for Architects
When designing large APIs or SaaS platforms, keep cross-cutting concerns (logging, security, caching, compression) in middleware, and business logic in endpoints. This keeps the pipeline clean and maintainable.
π Key Points
- Order of middleware matters.
- The pipeline behaves like an onion: requests go inward, responses go outward.
- Cross-cutting concerns belong in middleware.
- Business logic stays in controllers or endpoints.
| Kestrel In .NET Core :👈 | 👉:API Error Handling Best Practices |