liveBook · Manning

HttpContext

As explained in ASP.NET Core in Action, 3e

HttpContext is the ASP.NET Core object that represents the information and state associated with a single HTTP request and its response.

HttpContext holds the information associated with one HTTP request and its response. When a request arrives, ASP.NET Core’s web server creates the context from the incoming data and passes it into the application. Code handling the request uses it to decide what response to produce. section 3.1 section 1.4.2

Think of it as a storage box that travels with the request:

  • It starts with request details supplied by the web server and can hold request-specific services, loaded data, and errors. section 3.1
  • Middleware can read or change it. Each component sees changes made earlier in the pipeline; after a response is generated, components can inspect or modify it on the way back. section 4.1 section 4.2
  • A minimal API handler can receive HttpContext as a parameter. For common needs, HttpRequest and HttpResponse parameters provide narrower access to its Request and Response properties. section 7.6.1

A small middleware decision

A middleware component can check context.Request.Path. If the path starts with /ping, it sets a plain-text response and writes “pong”; otherwise, it passes the same context to the next middleware. For an ordinary application route, prefer endpoint routing over directly inspecting paths in middleware. section 36.1

Listing 36.1 StatusMiddleware to be tested, which returns a "pong" response
public class StatusMiddleware
{
    private readonly RequestDelegate _next;                      #1
    public StatusMiddleware(RequestDelegate next)                #1
    {
        _next = next;
    }
    public async Task Invoke(HttpContext context)                #2
    {
        if(context.Request.Path.StartsWithSegments("/ping"))     #3
        {                                                        #3
            context.Response.ContentType = "text/plain";         #3
            await context.Response.WriteAsync("pong");           #3
            return;                                              #3
        }                                                        #3
        await _next(context);                                    #4
    }
}

The listing shows both paths through the decision: write a response and stop, or call the next RequestDelegate with the HttpContext. section 36.1

For a middleware unit test, DefaultHttpContext supplies a context you can configure. Set Request.Path to a nonmatching value, invoke the middleware, and check that it called the next RequestDelegate. Because HttpContext exposes many request and response details, such tests can become tightly coupled to the middleware implementation. section 36.1

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains creation, stored details, pipeline behavior, handler access, and a unit-test setup without examining HttpContext implementation internals.
Examples
Shows a /ping middleware response, a nonmatching-path unit test, and a minimal API handler parameter.
What it assumes
Builds from HTTP request-and-response flow; the handler and middleware examples use C#.
What you can do after
Apply it via ASP.NET Core middleware or minimal API handlers to read request details, produce responses, and test middleware behavior.
How it connects
Connects HTTP requests and responses with Kestrel, middleware, endpoint handlers, HttpRequest, and HttpResponse.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.