Search Results for

    Show / Hide Table of Contents

    Class HttpResponseExtensions

    Inheritance
    object
    HttpResponseExtensions
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: FastEndpoints
    Assembly: FastEndpoints.dll
    Syntax
    public static class HttpResponseExtensions

    Methods

    SendAcceptedAtAsync(HttpResponse, string, object?, object?, JsonSerializerContext?, bool, CancellationToken)

    send a 202 accepted 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 SendAcceptedAtAsync(this HttpResponse rsp, string endpointName, object? routeValues = null, object? responseBody = null, 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 an absolute url instead of relative url for the location header

    CancellationToken cancellation

    optional cancellation token. if not specified, the HttpContext.RequestAborted token is used.

    Returns
    Type Description
    Task

    SendAcceptedAtAsync<TEndpoint>(HttpResponse, object?, object?, Http?, int?, JsonSerializerContext?, bool, CancellationToken)

    send a 202 accepted 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 SendAcceptedAtAsync<TEndpoint>(this HttpResponse rsp, object? routeValues = null, object? responseBody = null, 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 an absolute url instead of relative url for the location header

    CancellationToken cancellation

    optional cancellation token. if not specified, the HttpContext.RequestAborted token is used.

    Returns
    Type Description
    Task
    Type Parameters
    Name Description
    TEndpoint

    the type of the endpoint where the resource can be retrieved from

    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 HttpContext.RequestAborted token is used.

    Returns
    Type Description
    Task
    Type Parameters
    Name Description
    TResponse

    SendAtAsync(HttpResponse, int, string, object?, bool, string, object?, JsonSerializerContext?, CancellationToken)

    send a custom 20X (accepted/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 SendAtAsync(this HttpResponse rsp, int statusCode, string endpointName, object? routeValues = null, bool generateAbsoluteUrl = false, string contentType = "application/json", object? responseBody = null, JsonSerializerContext? jsonSerializerContext = null, CancellationToken cancellation = default)
    Parameters
    Type Name Description
    HttpResponse rsp
    int statusCode

    the http status code to send

    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

    bool generateAbsoluteUrl

    set to true for generating an absolute url instead of relative url for the location header

    string contentType

    the content type for the response

    object responseBody

    the content to be serialized in the response body

    JsonSerializerContext jsonSerializerContext

    json serializer context if code generation is used

    CancellationToken cancellation

    optional cancellation token. if not specified, the HttpContext.RequestAborted token is used.

    Returns
    Type Description
    Task

    SendAtAsync<TEndpoint>(HttpResponse, int, object?, bool, string, object?, Http?, int?, JsonSerializerContext?, CancellationToken)

    send a custom 20X (accepted/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 SendAtAsync<TEndpoint>(this HttpResponse rsp, int statusCode, object? routeValues = null, bool generateAbsoluteUrl = false, string contentType = "application/json", object? responseBody = null, Http? verb = null, int? routeNumber = null, JsonSerializerContext? jsonSerializerContext = null, CancellationToken cancellation = default) where TEndpoint : IEndpoint
    Parameters
    Type Name Description
    HttpResponse rsp
    int statusCode

    the http status code to send

    object routeValues

    a route values object with key/value pairs of route information

    bool generateAbsoluteUrl

    set to true for generating an absolute url instead of relative url for the location header

    string contentType

    the content type for the response

    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

    CancellationToken cancellation

    optional cancellation token. if not specified, the HttpContext.RequestAborted token is used.

    Returns
    Type Description
    Task
    Type Parameters
    Name Description
    TEndpoint

    the type of the endpoint where the resource can be retrieved from

    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 HttpContext.RequestAborted token is used.

    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 = null, object? responseBody = null, 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 an absolute url instead of relative url for the location header

    CancellationToken cancellation

    optional cancellation token. if not specified, the HttpContext.RequestAborted token is used.

    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 = null, object? responseBody = null, 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 an absolute url instead of relative url for the location header

    CancellationToken cancellation

    optional cancellation token. if not specified, the HttpContext.RequestAborted token is used.

    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 HttpContext.RequestAborted token is used.

    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 HttpContext.RequestAborted token is used.

    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 HttpContext.RequestAborted token is used.

    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 HttpContext.RequestAborted token is used.

    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 HttpContext.RequestAborted token is used.

    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 HttpContext.RequestAborted token is used.

    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 HttpContext.RequestAborted token is used.

    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 HttpContext.RequestAborted token is used.

    Returns
    Type Description
    Task

    SendOkAsync(HttpResponse, CancellationToken)

    send an http 200 ok response without a 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 HttpContext.RequestAborted token is used.

    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 HttpContext.RequestAborted token is used.

    Returns
    Type Description
    Task
    Type Parameters
    Name Description
    TResponse

    SendRedirectAsync(HttpResponse, string, bool, bool)

    send a 302/301 redirect response

    Declaration
    public static Task SendRedirectAsync(this HttpResponse rsp, string location, bool isPermanent, bool allowRemoteRedirects = false)
    Parameters
    Type Name Description
    HttpResponse rsp
    string location

    the location to redirect to

    bool isPermanent

    set to true for a 301 redirect. 302 is the default.

    bool allowRemoteRedirects

    set to true if it's ok to redirect to remote addresses, which is prone to open redirect attacks.

    Returns
    Type Description
    Task
    Exceptions
    Type Condition
    InvalidOperationException

    thrown if allowRemoteRedirects is not set to true and the supplied url is not local

    SendResultAsync(HttpResponse, IResult)

    execute and send any IResult produced by the Results or TypedResults classes in minimal apis.

    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 from:

    - Results.Ok();
    - TypedResults.NotFound();
    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 HttpContext.RequestAborted token is used.

    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; charset=utf-8", 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 HttpContext.RequestAborted token is used.

    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 HttpContext.RequestAborted token is used.

    Returns
    Type Description
    Task
    In this article
    Back to top Developed by Đĵ ΝιΓΞΗΛψΚ and contributors / Licensed under MIT / Website generated by DocFX