Unosquare
    Show / Hide Table of Contents

    Class HttpContextExtensions

    Provides extension methods for types implementing IHttpContext.

    Inheritance
    Object
    HttpContextExtensions
    Inherited Members
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: EmbedIO
    Syntax
    public static class HttpContextExtensions

    Methods

    GetImplementation(IHttpContext)

    Gets the underlying IHttpContextImpl interface of an IHttpContext.

    This API mainly supports the EmbedIO infrastructure; it is not intended to be used directly from your code, unless to fulfill very specific needs in the development of plug-ins (modules, etc.) for EmbedIO.

    Declaration
    public static IHttpContextImpl GetImplementation(this IHttpContext this)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext interface on which this method is called.

    Returns
    Type Description
    IHttpContextImpl

    The underlying IHttpContextImpl interface representing the HTTP context implementation.

    GetItem<T>(IHttpContext, Object)

    Gets the item associated with the specified key.

    Declaration
    public static T GetItem<T>(this IHttpContext this, object key)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    Object key

    The key whose value to get from the Items dictionary.

    Returns
    Type Description
    T

    The item associated with the specified key, if the key is found in Items and the associated value is of type T; otherwise, the default value for T.

    Type Parameters
    Name Description
    T

    The desired type of the item.

    GetRequestBodyAsByteArrayAsync(IHttpContext)

    Asynchronously retrieves the request body as an array of langword_csharp_bytes.

    Declaration
    public static Task<byte[]> GetRequestBodyAsByteArrayAsync(this IHttpContext this)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    Returns
    Type Description
    Task<Byte[]>

    A Task, representing the ongoing operation, whose result will be an array of Bytes containing the request body.

    GetRequestBodyAsMemoryStreamAsync(IHttpContext)

    Asynchronously buffers the request body into a read-only .

    Declaration
    public static Task<MemoryStream> GetRequestBodyAsMemoryStreamAsync(this IHttpContext this)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    Returns
    Type Description
    Task<MemoryStream>

    A Task, representing the ongoing operation, whose result will be a read-only containing the request body.

    GetRequestBodyAsStringAsync(IHttpContext)

    Asynchronously retrieves the request body as a string.

    Declaration
    public static Task<string> GetRequestBodyAsStringAsync(this IHttpContext this)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    Returns
    Type Description
    Task<String>

    A Task, representing the ongoing operation, whose result will be a langword_csharp_string representation of the request body.

    GetRequestDataAsync<TData>(IHttpContext)

    Asynchronously deserializes a request body, using the default request deserializer.

    As of EmbedIO version 3.0, the default response serializer has the same behavior of JSON request parsing methods of version 2.

    Declaration
    public static Task<TData> GetRequestDataAsync<TData>(this IHttpContext this)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    Returns
    Type Description
    Task<TData>

    A Task, representing the ongoing operation, whose result will be the deserialized data.

    Type Parameters
    Name Description
    TData

    The expected type of the deserialized data.

    GetRequestDataAsync<TData>(IHttpContext, RequestDeserializerCallback<TData>)

    Asynchronously deserializes a request body, using the specified request deserializer.

    Declaration
    public static Task<TData> GetRequestDataAsync<TData>(this IHttpContext this, RequestDeserializerCallback<TData> deserializer)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    RequestDeserializerCallback<TData> deserializer

    A RequestDeserializerCallback<TData> used to deserialize the request body.

    Returns
    Type Description
    Task<TData>

    A Task, representing the ongoing operation, whose result will be the deserialized data.

    Type Parameters
    Name Description
    TData

    The expected type of the deserialized data.

    GetRequestFormDataAsync(IHttpContext)

    Asynchronously parses a request body in application/x-www-form-urlencoded format.

    Declaration
    public static Task<NameValueCollection> GetRequestFormDataAsync(this IHttpContext this)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    Returns
    Type Description
    Task<NameValueCollection>

    A Task, representing the ongoing operation, whose result will be a read-only of form field names and values.

    Remarks

    This method may safely be called more than once for the same IHttpContext: it will return the same collection instead of trying to parse the request body again.

    GetRequestQueryData(IHttpContext)

    Parses a request URL query. Note that this is different from getting the QueryString property, in that fields without an equal sign are treated as if they have an empty value, instead of their keys being grouped as values of the null key.

    Declaration
    public static NameValueCollection GetRequestQueryData(this IHttpContext this)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    Returns
    Type Description
    NameValueCollection

    A read-only .

    Remarks

    This method may safely be called more than once for the same IHttpContext: it will return the same collection instead of trying to parse the request body again.

    OpenRequestStream(IHttpContext)

    Wraps the request input stream and returns a that can be used directly.

    Decompression of compressed request bodies is implemented if specified in the web server's options.

    Declaration
    public static Stream OpenRequestStream(this IHttpContext this)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    Returns
    Type Description
    Stream

    A that can be used to write response data.

    This stream MUST be disposed when finished writing.

    See Also
    OpenRequestText(IHttpContext)
    SupportCompressedRequests

    OpenRequestText(IHttpContext)

    Wraps the request input stream and returns a that can be used directly.

    Decompression of compressed request bodies is implemented if specified in the web server's options.

    If the request does not specify a content encoding, UTF-8 is used by default.

    Declaration
    public static TextReader OpenRequestText(this IHttpContext this)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    Returns
    Type Description
    TextReader

    A that can be used to read the request body as text.

    This reader MUST be disposed when finished reading.

    See Also
    OpenRequestStream(IHttpContext)
    SupportCompressedRequests

    OpenResponseStream(IHttpContext, Boolean, Boolean)

    Wraps the response output stream and returns a that can be used directly.

    Optional buffering is applied, so that the response may be sent as one (thereby properly setting the Content-Length header) instead of using chunked transfer.

    Proactive negotiation is performed to select the best compression method supported by the client.

    Declaration
    public static Stream OpenResponseStream(this IHttpContext this, bool buffered = false, bool preferCompression = true)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    Boolean buffered

    If set to true, the data to send will be collected in memory and sent all at once when the returned is disposed, setting the Content-Length response header; if set to false (the default), chunked transfer will be used.

    Boolean preferCompression

    true if sending compressed data is preferred over sending non-compressed data; otherwise, false.

    Returns
    Type Description
    Stream

    A that can be used to write response data.

    This stream MUST be disposed when finished writing.

    See Also
    OpenResponseText(IHttpContext, Nullable<Encoding>, Boolean, Boolean)

    OpenResponseText(IHttpContext, Nullable<Encoding>, Boolean, Boolean)

    Wraps the response output stream and returns a that can be used directly.

    Optional buffering is applied, so that the response may be sent as one (thereby properly setting the Content-Length header) instead of using chunked transfer.

    Proactive negotiation is performed to select the best compression method supported by the client.

    Declaration
    public static TextWriter OpenResponseText(this IHttpContext this, Encoding? encoding = default(Encoding? ), bool buffered = false, bool preferCompression = true)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    Nullable<Encoding> encoding

    The to use to convert text to data bytes. By default, UTF-8 is used.

    Boolean buffered

    If set to true, sent data is collected in a and sent all at once when the returned is disposed; if set to false (the default), chunked transfer will be used.

    Boolean preferCompression

    true if sending compressed data is preferred over sending non-compressed data; otherwise, false.

    Returns
    Type Description
    TextWriter

    A that can be used to write response data.

    This writer MUST be disposed when finished writing.

    See Also
    OpenResponseStream(IHttpContext, Boolean, Boolean)

    Redirect(IHttpContext, String, Int32)

    Sets a redirection status code and adds a Location header to the response.

    Declaration
    public static void Redirect(this IHttpContext this, string location, int statusCode = default(int))
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext interface on which this method is called.

    String location

    The URL to which the user agent should be redirected.

    Int32 statusCode

    The status code to set on the response.

    SendDataAsync(IHttpContext, ResponseSerializerCallback, Object)

    Asynchronously sends serialized data as a response, using the specified response serializer.

    As of EmbedIO version 3.0, the default response serializer has the same behavior of JSON response methods of version 2.

    Declaration
    public static Task SendDataAsync(this IHttpContext this, ResponseSerializerCallback serializer, object data)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext interface on which this method is called.

    ResponseSerializerCallback serializer

    A ResponseSerializerCallback used to prepare the response.

    Object data

    The data to serialize.

    Returns
    Type Description
    Task

    A representing the ongoing operation.

    See Also
    SendDataAsync(IHttpContext, ResponseSerializerCallback, Object)
    Default

    SendDataAsync(IHttpContext, Object)

    Asynchronously sends serialized data as a response, using the default response serializer.

    As of EmbedIO version 3.0, the default response serializer has the same behavior of JSON response methods of version 2.

    Declaration
    public static Task SendDataAsync(this IHttpContext this, object data)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext interface on which this method is called.

    Object data

    The data to serialize.

    Returns
    Type Description
    Task

    A representing the ongoing operation.

    See Also
    SendDataAsync(IHttpContext, ResponseSerializerCallback, Object)
    Default

    SendStandardHtmlAsync(IHttpContext, Int32)

    Asynchronously sends a standard HTML response for the specified status code.

    Declaration
    public static Task SendStandardHtmlAsync(this IHttpContext this, int statusCode)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext interface on which this method is called.

    Int32 statusCode

    The HTTP status code of the response.

    Returns
    Type Description
    Task

    A representing the ongoing operation.

    SendStandardHtmlAsync(IHttpContext, Int32, Nullable<Action<TextWriter>>)

    Asynchronously sends a standard HTML response for the specified status code.

    Declaration
    public static Task SendStandardHtmlAsync(this IHttpContext this, int statusCode, Action<TextWriter>? writeAdditionalHtml)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext interface on which this method is called.

    Int32 statusCode

    The HTTP status code of the response.

    Nullable<Action<TextWriter>> writeAdditionalHtml

    A callback function that may write additional HTML code to a representing the response output. If not null, the callback is called immediately before closing the HTML body tag.

    Returns
    Type Description
    Task

    A representing the ongoing operation.

    See Also
    SendStandardHtmlAsync(IHttpContext, Int32)

    SendStringAsync(IHttpContext, String, String, Encoding)

    Asynchronously sends a string as response.

    Declaration
    public static Task SendStringAsync(this IHttpContext this, string content, string contentType, Encoding encoding)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpResponse interface on which this method is called.

    String content

    The response content.

    String contentType

    The MIME type of the content. If null, the content type will not be set.

    Encoding encoding

    The to use.

    Returns
    Type Description
    Task

    A representing the ongoing operation.

    TryGetItem<T>(IHttpContext, Object, out T)

    Gets the item associated with the specified key.

    Declaration
    public static bool TryGetItem<T>(this IHttpContext this, object key, out T value)
    Parameters
    Type Name Description
    IHttpContext this

    The IHttpContext on which this method is called.

    Object key

    The key whose value to get from the Items dictionary.

    T value

    When this method returns, the item associated with the specified key, if the key is found in Items and the associated value is of type T; otherwise, the default value for T.

    This parameter is passed uninitialized.

    Returns
    Type Description
    Boolean

    true if the item is found and is of type T; otherwise, false.

    Type Parameters
    Name Description
    T

    The desired type of the item.

    Comments

    Back to top Copyright © 2017-2019 Unosquare