Class HttpListenerRequestContext

Namespace
Cratis.Arc.Http
Assembly
Cratis.Arc.Core.dll
public class HttpListenerRequestContext : IHttpRequestContext
Inheritance
HttpListenerRequestContext
Implements
Inherited Members
Extension Methods

Remarks

Initializes a new instance of the HttpListenerRequestContext class.

Constructors

HttpListenerRequestContext(HttpListenerContext, IServiceProvider)

public HttpListenerRequestContext(HttpListenerContext context, IServiceProvider serviceProvider)

Parameters

context HttpListenerContext

The HttpListenerContext.

serviceProvider IServiceProvider

The IServiceProvider.

Remarks

Initializes a new instance of the HttpListenerRequestContext class.

Properties

ContentType

Gets the content type of the response.

public string? ContentType { get; set; }

Property Value

string

Cookies

Gets the request cookies.

public IReadOnlyDictionary<string, string> Cookies { get; }

Property Value

IReadOnlyDictionary<string, string>

Headers

Gets the request headers.

public IReadOnlyDictionary<string, string> Headers { get; }

Property Value

IReadOnlyDictionary<string, string>

Host

Gets the request host (without port).

public string Host { get; }

Property Value

string

IsHttps

Gets a value indicating whether the request is HTTPS.

public bool IsHttps { get; }

Property Value

bool

Items

Gets a dictionary for storing arbitrary data during the request lifecycle.

public IDictionary<object, object?> Items { get; }

Property Value

IDictionary<object, object>

Method

Gets the HTTP method (GET, POST, etc.).

public string Method { get; }

Property Value

string

Path

Gets the request path.

public string Path { get; }

Property Value

string

Query

Gets the query string parameters.

public IReadOnlyDictionary<string, string> Query { get; }

Property Value

IReadOnlyDictionary<string, string>

RequestAborted

Gets the cancellation token for the request.

public CancellationToken RequestAborted { get; }

Property Value

CancellationToken

RequestServices

Gets the service provider for the request scope.

public IServiceProvider RequestServices { get; }

Property Value

IServiceProvider

StatusCode

Gets the status code of the response.

public int StatusCode { get; set; }

Property Value

int

User

Gets or sets the user associated with the request.

public ClaimsPrincipal User { get; set; }

Property Value

ClaimsPrincipal

WebSockets

Gets the WebSocket context for this request.

public IWebSocketContext WebSockets { get; }

Property Value

IWebSocketContext

Methods

AppendCookie(string, string, CookieOptions)

Appends a cookie to the response.

public void AppendCookie(string key, string value, CookieOptions options)

Parameters

key string

The cookie name.

value string

The cookie value.

options CookieOptions

The cookie options.

GetResponseHeader(string)

Gets the current value of a response header.

public string? GetResponseHeader(string name)

Parameters

name string

Header name.

Returns

string

The header value, with multiple values combined as a comma-separated list, or null if the header is not set.

ReadBodyAsJson(Type, CancellationToken)

Reads the request body and deserializes it as JSON.

public Task<object?> ReadBodyAsJson(Type type, CancellationToken cancellationToken = default)

Parameters

type Type

The type to deserialize to.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<object>

The deserialized object.

RemoveCookie(string)

Removes a cookie from the response.

public void RemoveCookie(string key)

Parameters

key string

The cookie name.

SetResponseHeader(string, string)

Sets a response header.

public void SetResponseHeader(string name, string value)

Parameters

name string

Header name.

value string

Header value.

SetStatusCode(int)

Sets the HTTP response status code.

public void SetStatusCode(int statusCode)

Parameters

statusCode int

The status code to set.

Write(string, CancellationToken)

Writes text to the response.

public Task Write(string text, CancellationToken cancellationToken = default)

Parameters

text string

The text to write.

cancellationToken CancellationToken

Cancellation token.

Returns

Task

A task representing the async operation.

WriteBytes(byte[], CancellationToken)

Writes bytes to the response.

public Task WriteBytes(byte[] data, CancellationToken cancellationToken = default)

Parameters

data byte[]

The byte data to write.

cancellationToken CancellationToken

Cancellation token.

Returns

Task

A task representing the async operation.

WriteResponseAsJson(object?, Type, CancellationToken)

Writes JSON to the response.

public Task WriteResponseAsJson(object? value, Type type, CancellationToken cancellationToken = default)

Parameters

value object

The value to serialize.

type Type

The type of the value.

cancellationToken CancellationToken

Cancellation token.

Returns

Task

A task representing the async operation.

WriteStream(Stream, CancellationToken)

Writes a stream to the response.

public Task WriteStream(Stream stream, CancellationToken cancellationToken = default)

Parameters

stream Stream

The stream to write.

cancellationToken CancellationToken

Cancellation token.

Returns

Task

A task representing the async operation.