Class HttpContextExtensions
Provides extension methods for types implementing IHttpContext.
Inherited Members
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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
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 This stream MUST be disposed when finished writing. |
See Also
OpenRequestText(IHttpContext)
Wraps the request input stream and returns a
Decompression of compressed request bodies is implemented if specified in the web server's options.
If the request does not specify a content encoding,
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 This reader MUST be disposed when finished reading. |
See Also
OpenResponseStream(IHttpContext, Boolean, Boolean)
Wraps the response output stream and returns a
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 |
Boolean | preferCompression | true if sending compressed data is preferred over sending non-compressed data; otherwise, false. |
Returns
Type | Description |
---|---|
Stream | A This stream MUST be disposed when finished writing. |
See Also
OpenResponseText(IHttpContext, Nullable<Encoding>, Boolean, Boolean)
Wraps the response output stream and returns a
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 |
Boolean | buffered | If set to true, sent data is collected
in a |
Boolean | preferCompression | true if sending compressed data is preferred over sending non-compressed data; otherwise, false. |
Returns
Type | Description |
---|---|
TextWriter | A This writer MUST be disposed when finished writing. |
See Also
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 |
See Also
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 |
See Also
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 |
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 |
Returns
Type | Description |
---|---|
Task | A |
See Also
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 |
Returns
Type | Description |
---|---|
Task | A |
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 This parameter is passed uninitialized. |
Returns
Type | Description |
---|---|
Boolean | true if the item is found and is of type |
Type Parameters
Name | Description |
---|---|
T | The desired type of the item. |