Class HttpResponseExtensions
Inherited Members
Namespace: FastEndpoints
Assembly: FastEndpoints.dll
Syntax
public static class HttpResponseExtensions
Methods
SendAsync<TResponse>(HttpResponse, TResponse, int, JsonSerializerContext?, CancellationToken)
send the supplied response dto serialized as json to the client.
Declaration
public static Task SendAsync<TResponse>(this HttpResponse rsp, TResponse response, int statusCode = 200, JsonSerializerContext? jsonSerializerContext = null, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
TResponse | response | the object to serialize to json |
int | statusCode | optional custom http status code |
JsonSerializerContext | jsonSerializerContext | json serializer context if code generation is used |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
Type Parameters
Name | Description |
---|---|
TResponse |
SendBytesAsync(HttpResponse, byte[], string?, string, DateTimeOffset?, bool, CancellationToken)
send a byte array to the client
Declaration
public static Task SendBytesAsync(this HttpResponse rsp, byte[] bytes, string? fileName = null, string contentType = "application/octet-stream", DateTimeOffset? lastModified = null, bool enableRangeProcessing = false, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
byte[] | bytes | the bytes to send |
string | fileName | |
string | contentType | optional content type to set on the http response |
DateTimeOffset? | lastModified | optional last modified date-time-offset for the data stream |
bool | enableRangeProcessing | optional switch for enabling range processing |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendCreatedAtAsync(HttpResponse, string, object?, object?, JsonSerializerContext?, bool, CancellationToken)
send a 201 created response with a location header containing where the resource can be retrieved from.
WARNING: this method is only supported on single verb/route endpoints. it will not produce a `Location` header if used in a multi verb or multi route endpoint.
Declaration
public static Task SendCreatedAtAsync(this HttpResponse rsp, string endpointName, object? routeValues, object? responseBody, JsonSerializerContext? jsonSerializerContext = null, bool generateAbsoluteUrl = false, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
string | endpointName | the name of the endpoint to use for link generation (openapi route id) |
object | routeValues | a route values object with key/value pairs of route information |
object | responseBody | the content to be serialized in the response body |
JsonSerializerContext | jsonSerializerContext | json serializer context if code generation is used |
bool | generateAbsoluteUrl | set to true for generating a absolute url instead of relative url for the location header |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendCreatedAtAsync<TEndpoint>(HttpResponse, object?, object?, Http?, int?, JsonSerializerContext?, bool, CancellationToken)
send a 201 created response with a location header containing where the resource can be retrieved from.
HINT: if pointing to an endpoint with multiple verbs, make sure to specify the 'verb' argument and if pointing to a multi route endpoint, specify the 'routeNumber' argument.
WARNING: this overload will not add a location header if you've set a custom endpoint name using .WithName() method. use the other overload that accepts a string endpoint name instead.
Declaration
public static Task SendCreatedAtAsync<TEndpoint>(this HttpResponse rsp, object? routeValues, object? responseBody, Http? verb = null, int? routeNumber = null, JsonSerializerContext? jsonSerializerContext = null, bool generateAbsoluteUrl = false, CancellationToken cancellation = default) where TEndpoint : IEndpoint
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
object | routeValues | a route values object with key/value pairs of route information |
object | responseBody | the content to be serialized in the response body |
Http? | verb | only useful when pointing to a multi verb endpoint |
int? | routeNumber | only useful when pointing to a multi route endpoint |
JsonSerializerContext | jsonSerializerContext | json serializer context if code generation is used |
bool | generateAbsoluteUrl | set to true for generating a absolute url instead of relative url for the location header |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
Type Parameters
Name | Description |
---|---|
TEndpoint | the type of the endpoint where the resource can be retrieved from |
SendEmptyJsonObject(HttpResponse, JsonSerializerContext?, CancellationToken)
send an empty json object in the body
Declaration
public static Task SendEmptyJsonObject(this HttpResponse rsp, JsonSerializerContext? jsonSerializerContext = null, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
JsonSerializerContext | jsonSerializerContext | json serializer context if code generation is used |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendErrorsAsync(HttpResponse, List<ValidationFailure>, int, JsonSerializerContext?, CancellationToken)
send a 400 bad request with error details of the current validation failures
Declaration
public static Task SendErrorsAsync(this HttpResponse rsp, List<ValidationFailure> failures, int statusCode = 400, JsonSerializerContext? jsonSerializerContext = null, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
List<ValidationFailure> | failures | the collection of failures |
int | statusCode | the http status code for the error response |
JsonSerializerContext | jsonSerializerContext | json serializer context if code generation is used |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendEventStreamAsync<T>(HttpResponse, string, IAsyncEnumerable<T>, CancellationToken)
start a "server-sent-events" data stream for the client asynchronously without blocking any threads
Declaration
public static Task SendEventStreamAsync<T>(this HttpResponse rsp, string eventName, IAsyncEnumerable<T> eventStream, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
string | eventName | the name of the event stream |
IAsyncEnumerable<T> | eventStream | an IAsyncEnumerable that is the source of the data |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
Type Parameters
Name | Description |
---|---|
T | the type of the objects being sent in the event stream |
SendFileAsync(HttpResponse, FileInfo, string, DateTimeOffset?, bool, CancellationToken)
send a file to the client
Declaration
public static Task SendFileAsync(this HttpResponse rsp, FileInfo fileInfo, string contentType = "application/octet-stream", DateTimeOffset? lastModified = null, bool enableRangeProcessing = false, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
FileInfo | fileInfo | |
string | contentType | optional content type to set on the http response |
DateTimeOffset? | lastModified | optional last modified date-time-offset for the data stream |
bool | enableRangeProcessing | optional switch for enabling range processing |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendForbiddenAsync(HttpResponse, CancellationToken)
send a 403 unauthorized response
Declaration
public static Task SendForbiddenAsync(this HttpResponse rsp, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendHeadersAsync(HttpResponse, Action<IHeaderDictionary>, int, CancellationToken)
send headers in response to a HEAD request
Declaration
public static Task SendHeadersAsync(this HttpResponse rsp, Action<IHeaderDictionary> headers, int statusCode = 200, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
Action<IHeaderDictionary> | headers | an action to be performed on the headers dictionary of the response |
int | statusCode | optional custom http status code |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendNoContentAsync(HttpResponse, CancellationToken)
send a 204 no content response
Declaration
public static Task SendNoContentAsync(this HttpResponse rsp, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendNotFoundAsync(HttpResponse, CancellationToken)
send a 404 not found response
Declaration
public static Task SendNotFoundAsync(this HttpResponse rsp, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendOkAsync(HttpResponse, CancellationToken)
send an http 200 ok response without any body
Declaration
public static Task SendOkAsync(this HttpResponse rsp, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendOkAsync<TResponse>(HttpResponse, TResponse, JsonSerializerContext?, CancellationToken)
send an http 200 ok response with the supplied response dto serialized as json to the client.
Declaration
public static Task SendOkAsync<TResponse>(this HttpResponse rsp, TResponse response, JsonSerializerContext? jsonSerializerContext = null, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
TResponse | response | the object to serialize to json |
JsonSerializerContext | jsonSerializerContext | json serializer context if code generation is used |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
Type Parameters
Name | Description |
---|---|
TResponse |
SendRedirectAsync(HttpResponse, string, bool, CancellationToken)
send a 302/301 redirect response
Declaration
public static Task SendRedirectAsync(this HttpResponse rsp, string location, bool isPermanant, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
string | location | the location to redirect to |
bool | isPermanant | set to true for a 301 redirect. 302 is the default. |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendResultAsync(HttpResponse, IResult)
Declaration
public static Task SendResultAsync(this HttpResponse rsp, IResult result)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
IResult | result | the IResult instance to execute such as:
|
Returns
Type | Description |
---|---|
Task |
SendStreamAsync(HttpResponse, Stream, string?, long?, string, DateTimeOffset?, bool, CancellationToken)
send the contents of a stream to the client
Declaration
public static Task SendStreamAsync(this HttpResponse rsp, Stream stream, string? fileName = null, long? fileLengthBytes = null, string contentType = "application/octet-stream", DateTimeOffset? lastModified = null, bool enableRangeProcessing = false, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
Stream | stream | the stream to read the data from |
string | fileName | and optional file name to set in the content-disposition header |
long? | fileLengthBytes | optional total size of the file/stream |
string | contentType | optional content type to set on the http response |
DateTimeOffset? | lastModified | optional last modified date-time-offset for the data stream |
bool | enableRangeProcessing | optional switch for enabling range processing |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendStringAsync(HttpResponse, string, int, string, CancellationToken)
send the supplied string content to the client.
Declaration
public static Task SendStringAsync(this HttpResponse rsp, string content, int statusCode = 200, string contentType = "text/plain", CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
string | content | the string to write to the response body |
int | statusCode | optional custom http status code |
string | contentType | optional content type header value |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |
SendUnauthorizedAsync(HttpResponse, CancellationToken)
send a 401 unauthorized response
Declaration
public static Task SendUnauthorizedAsync(this HttpResponse rsp, CancellationToken cancellation = default)
Parameters
Type | Name | Description |
---|---|---|
HttpResponse | rsp | |
CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
Task |