Where Is the Request Handling Logic Implemented in ASP.NET Core?

ASP.NET Core processes incoming HTTP requests through three tightly-coupled layers: the Kestrel server manages socket communication and HTTP parsing, the HttpApplication bridge adapts the server context to the application pipeline, and the middleware pipeline executes the composed RequestDelegate.

Understanding the request handling logic in ASP.NET Core requires tracing the path from raw TCP packets to controller actions. According to the dotnet/aspnetcore source code, the framework implements a layered architecture that separates network concerns from application logic. This article explores the concrete implementation files and method signatures that define how HTTP requests are processed.

The Three-Layer Architecture of ASP.NET Core Request Handling

The request handling logic is distributed across three distinct layers, each with specific responsibilities and source files.

Layer 1: The Server (Kestrel)

Kestrel acts as the edge server that implements the generic IHttpApplication<TContext> contract. In src/Servers/Kestrel/Core/src/KestrelServer.cs, the server reads raw bytes from the network socket and parses HTTP protocols. The parsing logic resides in src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs, which handles HTTP/1.x, HTTP/2, and HTTP/3, populating a feature collection that feeds into the HttpContext.

Layer 2: The Host-Application Bridge

The HttpApplication class in src/Hosting/GenericHost/src/HttpApplication.cs implements IHttpApplication<HttpContext>. This class serves as the adapter between the server and the application pipeline. Its ProcessRequestAsync method receives the HttpContext from Kestrel and forwards it to the user-configured middleware pipeline by invoking the compiled RequestDelegate.

Layer 3: The Middleware Pipeline

The pipeline itself is a compiled RequestDelegate. During startup, ApplicationBuilder.cs in src/Hosting/Abstractions/src/ApplicationBuilder.cs chains middleware components together. The RequestDelegateFactory.cs file in src/Http/Http.Extensions/src/ generates the final delegate that executes when the application processes a request, dispatching eventually to endpoints like controller actions or Razor pages.

How the Request Flow Works in ASP.NET Core

The request handling flow moves from network to application code through specific method calls. First, Kestrel creates the context using the internal HttpContextFactory. Then it invokes IHttpApplication<HttpContext>.ProcessRequestAsync. Finally, the HttpApplication instance dispatches to the RequestDelegate built from IApplicationBuilder.

// 1. Kestrel receives data → creates HttpContext
var httpContext = httpContextFactory.Create(contextFeatures);

// 2. Kestrel calls the HttpApplication implementation
await httpApplication.ProcessRequestAsync(httpContext);

// 3. HttpApplication invokes the compiled middleware pipeline
await _app(context);   // _app is the RequestDelegate built from IApplicationBuilder

This sequence mirrors the actual implementation where KestrelServer.cs handles socket operations, HttpApplication.cs provides the bridge, and the RequestDelegate represents the compiled middleware chain.

Key Source Files for Request Handling

The following files form the backbone of the ASP.NET Core request handling mechanism:

Summary

  • Kestrel handles raw network I/O and HTTP parsing in KestrelServer.cs and HttpProtocol.cs.
  • HttpApplication acts as the bridge between the server and middleware pipeline in HttpApplication.cs.
  • RequestDelegateFactory and ApplicationBuilder compile the middleware chain into an executable RequestDelegate.
  • The ProcessRequestAsync method in HttpApplication is the critical handoff point from server code to application code.

Frequently Asked Questions

What is the role of IHttpApplication in ASP.NET Core?

IHttpApplication<TContext> defines the contract that servers like Kestrel use to invoke application logic. The HttpApplication class implements this interface in src/Hosting/GenericHost/src/HttpApplication.cs to bridge the server-created HttpContext with the middleware pipeline, specifically through its ProcessRequestAsync method.

How does Kestrel parse incoming HTTP requests?

Kestrel delegates protocol parsing to classes in src/Servers/Kestrel/Core/src/Internal/Http/. The HttpProtocol.cs file handles HTTP/1.x, HTTP/2, and HTTP/3 parsing, populating a feature collection that becomes the foundation for the HttpContext exposed to application code.

Where is the middleware pipeline built and executed?

The pipeline is constructed during host startup by ApplicationBuilder.cs in src/Hosting/Abstractions/src/. It is compiled into a RequestDelegate by RequestDelegateFactory.cs. Execution occurs when HttpApplication.ProcessRequestAsync invokes this delegate, passing the HttpContext through each middleware component in sequence.

What is the difference between HttpApplication and RequestDelegate?

HttpApplication is the adapter class that implements IHttpApplication<HttpContext> and receives the initial call from the server. A RequestDelegate is a compiled function representing the entire middleware chain that HttpApplication invokes to process the request through the configured pipeline.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →