Class HttpResponseExtensions
Inheritance
Inherited Members
Namespace: FastEndpoints
Assembly: FastEndpoints.dll
Syntax
public static class HttpResponseExtensions
Methods
SendAsync<TResponse>(HttpResponse, TResponse, Int32, 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(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
TResponse | response | the object to serialize to json |
System.Int32 | statusCode | optional custom http status code |
System.Text.Json.Serialization.JsonSerializerContext | jsonSerializerContext | json serializer context if code generation is used |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
Type Parameters
Name | Description |
---|---|
TResponse |
SendBytesAsync(HttpResponse, Byte[], String, String, Nullable<DateTimeOffset>, Boolean, CancellationToken)
send a byte array to the client
Declaration
public static async Task SendBytesAsync(this HttpResponse rsp, byte[] bytes, string fileName = null, string contentType = "application/octet-stream", DateTimeOffset? lastModified = null, bool enableRangeProcessing = false, CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.Byte[] | bytes | the bytes to send |
System.String | fileName | |
System.String | contentType | optional content type to set on the http response |
System.Nullable<System.DateTimeOffset> | lastModified | optional last modified date-time-offset for the data stream |
System.Boolean | enableRangeProcessing | optional switch for enabling range processing |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
SendCreatedAtAsync(HttpResponse, String, Object, Object, JsonSerializerContext, Boolean, 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(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.String | endpointName | the name of the endpoint to use for link generation (openapi route id) |
System.Object | routeValues | a route values object with key/value pairs of route information |
System.Object | responseBody | the content to be serialized in the response body |
System.Text.Json.Serialization.JsonSerializerContext | jsonSerializerContext | json serializer context if code generation is used |
System.Boolean | generateAbsoluteUrl | set to true for generating a absolute url instead of relative url for the location header |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
SendCreatedAtAsync<TEndpoint>(HttpResponse, Object, Object, Nullable<Http>, Nullable<Int32>, JsonSerializerContext, Boolean, 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(CancellationToken))
where TEndpoint : IEndpoint
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.Object | routeValues | a route values object with key/value pairs of route information |
System.Object | responseBody | the content to be serialized in the response body |
System.Nullable<Http> | verb | only useful when pointing to a multi verb endpoint |
System.Nullable<System.Int32> | routeNumber | only useful when pointing to a multi route endpoint |
System.Text.Json.Serialization.JsonSerializerContext | jsonSerializerContext | json serializer context if code generation is used |
System.Boolean | generateAbsoluteUrl | set to true for generating a absolute url instead of relative url for the location header |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.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(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.Text.Json.Serialization.JsonSerializerContext | jsonSerializerContext | json serializer context if code generation is used |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
SendErrorsAsync(HttpResponse, List<ValidationFailure>, Int32, 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(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.Collections.Generic.List<FluentValidation.Results.ValidationFailure> | failures | the collection of failures |
System.Int32 | statusCode | the http status code for the error response |
System.Text.Json.Serialization.JsonSerializerContext | jsonSerializerContext | json serializer context if code generation is used |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.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 async Task SendEventStreamAsync<T>(this HttpResponse rsp, string eventName, IAsyncEnumerable<T> eventStream, CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.String | eventName | the name of the event stream |
System.Collections.Generic.IAsyncEnumerable<T> | eventStream | an IAsyncEnumerable that is the source of the data |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
Type Parameters
Name | Description |
---|---|
T | the type of the objects being sent in the event stream |
SendFileAsync(HttpResponse, FileInfo, String, Nullable<DateTimeOffset>, Boolean, 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(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.IO.FileInfo | fileInfo | |
System.String | contentType | optional content type to set on the http response |
System.Nullable<System.DateTimeOffset> | lastModified | optional last modified date-time-offset for the data stream |
System.Boolean | enableRangeProcessing | optional switch for enabling range processing |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
SendForbiddenAsync(HttpResponse, CancellationToken)
send a 403 unauthorized response
Declaration
public static Task SendForbiddenAsync(this HttpResponse rsp, CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
SendHeadersAsync(HttpResponse, Action<IHeaderDictionary>, Int32, 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(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.Action<Microsoft.AspNetCore.Http.IHeaderDictionary> | headers | an action to be performed on the headers dictionary of the response |
System.Int32 | statusCode | optional custom http status code |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
SendNoContentAsync(HttpResponse, CancellationToken)
send a 204 no content response
Declaration
public static Task SendNoContentAsync(this HttpResponse rsp, CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
SendNotFoundAsync(HttpResponse, CancellationToken)
send a 404 not found response
Declaration
public static Task SendNotFoundAsync(this HttpResponse rsp, CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
SendOkAsync(HttpResponse, CancellationToken)
send an http 200 ok response without any body
Declaration
public static Task SendOkAsync(this HttpResponse rsp, CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.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(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
TResponse | response | the object to serialize to json |
System.Text.Json.Serialization.JsonSerializerContext | jsonSerializerContext | json serializer context if code generation is used |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
Type Parameters
Name | Description |
---|---|
TResponse |
SendRedirectAsync(HttpResponse, String, Boolean, CancellationToken)
send a 301/302 redirect response
Declaration
public static Task SendRedirectAsync(this HttpResponse rsp, string location, bool isPermanant, CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.String | location | the location to redirect to |
System.Boolean | isPermanant | set to true for a 302 redirect. 301 is the default. |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
SendStreamAsync(HttpResponse, Stream, String, Nullable<Int64>, String, Nullable<DateTimeOffset>, Boolean, CancellationToken)
send the contents of a stream to the client
Declaration
public static async 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(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.IO.Stream | stream | the stream to read the data from |
System.String | fileName | and optional file name to set in the content-disposition header |
System.Nullable<System.Int64> | fileLengthBytes | optional total size of the file/stream |
System.String | contentType | optional content type to set on the http response |
System.Nullable<System.DateTimeOffset> | lastModified | optional last modified date-time-offset for the data stream |
System.Boolean | enableRangeProcessing | optional switch for enabling range processing |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
SendStringAsync(HttpResponse, String, Int32, 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(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.String | content | the string to write to the response body |
System.Int32 | statusCode | optional custom http status code |
System.String | contentType | optional content type header value |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |
SendUnauthorizedAsync(HttpResponse, CancellationToken)
send a 401 unauthorized response
Declaration
public static Task SendUnauthorizedAsync(this HttpResponse rsp, CancellationToken cancellation = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
Microsoft.AspNetCore.Http.HttpResponse | rsp | |
System.Threading.CancellationToken | cancellation | optional cancellation token. if not specified, the |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task |