Skip to main content

Response class

The HTTP response an HttpHandlerAttribute method returns.

public sealed class Response

Public Members​

namedescription
static Bytes(…)Creates a response from bytes with the content type you name, such as image/png.
static Http(…)Creates a response with exactly the status, body and headers you give it.
static Json(…)Creates a JSON response from text that is already JSON, sent as UTF-8 with the content type application/json.
static Raw(…)Creates a response from bytes, sent without a content type.
static Text(…)Creates a plain-text response, sent as UTF-8 with the content type text/plain; charset=utf-8.
static Stream(…)Starts a response whose body is sent in pieces as the handler produces them, and returns the writer for those pieces.

Remarks​

Create one with the static method that fits what you are sending:

  • Text for plain text.
  • Json for JSON you have already serialized to a string.
  • Bytes for binary content with a content type, such as an image.
  • Raw for bytes sent without a content type.
  • Http to choose every header yourself, as a redirect or a cookie needs.
  • Stream to send the body in pieces as it is produced, instead of building it all first.

Bytes, Raw and Http keep the array they are given rather than copying it, so do not change it after the call.

An ordinary unhandled handler exception produces status 500 if the response has not started, or aborts it otherwise. Returning a response whose status is below 200 or whose headers are not valid HTTP produces status 502.

An ASP.NET application does not use this type: it writes to HttpContext.Response as usual.

See Also​