liveBook · Manning

HTTP response

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

HttpResponse is the ASP.NET Core object representing the HTTP response sent to a client, including its status code, headers, content type, and body.

HttpResponse represents the response sent from an ASP.NET Core minimal API endpoint to its client. It exposes the response status code, content type, and body, letting an endpoint customize framework defaults when needed. section 5.3.1 section 7.6.1

Minimal APIs inject HttpResponse when it appears as a handler parameter. Use the narrower type when the handler needs response details; use HttpContext when it needs both request and response information. section 7.6.1

Choose the simplest response approach that fits the endpoint:

  • Return an object for JSON, a string for text/plain, or an IResult such as Results.NotFound() for a common status code. section 5.6
  • Inject HttpResponse when requirements are more specific, then set StatusCode, ContentType, and the body directly. section 5.3.1

Writing a plain-text response

app.MapGet("/teapot", (HttpResponse response) => { response.StatusCode = 418; response.ContentType = MediaTypeNames.Text.Plain; return response.WriteAsync("I'm a teapot!"); }); This endpoint returns status 418 with a plain-text body. Manual construction is useful for specialized responses, but common results are usually shorter with Results or TypedResults. section 5.3.1

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

How deep it goes
Explains handler injection and manually sets status code, content type, and body, while deferring broader response features to helpers.
Examples
Demonstrates a /teapot endpoint returning status 418, text/plain content, and an I'm a teapot body.
What it assumes
Assumes basic minimal API endpoint handlers and contrasts HttpResponse with HttpContext without requiring deeper framework knowledge.
What you can do after
Build it from scratch by injecting HttpResponse and writing a customized minimal API response.
How it connects
Connects HttpResponse with minimal APIs, HttpContext, Results, TypedResults, HTTP status codes, content types, and response bodies.

Also explained in

Compare how 4 books explain HTTP response →

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.