Class HttpListenerEndpointMapper

Namespace
Cratis.Arc.Http
Assembly
Cratis.Arc.Core.dll

Implementation of IEndpointMapper using HttpListener.

public class HttpListenerEndpointMapper : IEndpointMapper, IDisposable
Inheritance
HttpListenerEndpointMapper
Implements
Inherited Members
Extension Methods

Constructors

HttpListenerEndpointMapper(ILogger<HttpListenerEndpointMapper>, params string[])

Initializes a new instance of the HttpListenerEndpointMapper class.

public HttpListenerEndpointMapper(ILogger<HttpListenerEndpointMapper> logger, params string[] prefixes)

Parameters

logger ILogger<HttpListenerEndpointMapper>

The logger for the endpoint mapper.

prefixes string[]

Optional HTTP prefixes to listen on. Defaults to http://localhost:5001/.

Properties

Routes

Gets all registered routes with their metadata.

public IEnumerable<RouteInfo> Routes { get; }

Property Value

IEnumerable<RouteInfo>

A collection of route information.

Methods

Dispose()

public void Dispose()

EndpointExists(string)

Checks if an endpoint with the given name already exists.

public bool EndpointExists(string name)

Parameters

name string

The endpoint name.

Returns

bool

True if the endpoint exists.

MapFallbackToFile(string)

Sets a fallback file to serve when no route or static file matches the request. This is typically used for Single Page Applications (SPAs) to serve index.html for client-side routing.

public void MapFallbackToFile(string filePath)

Parameters

filePath string

The path to the fallback file, relative to the first static file configuration's FileSystemPath.

MapGet(string, Func<IHttpRequestContext, Task>, EndpointMetadata?)

Maps a GET endpoint.

public void MapGet(string pattern, Func<IHttpRequestContext, Task> handler, EndpointMetadata? metadata = null)

Parameters

pattern string

The route pattern.

handler Func<IHttpRequestContext, Task>

The handler for the request.

metadata EndpointMetadata

Optional metadata for the endpoint (tags, name, etc.).

MapMethod(string, string, Func<IHttpRequestContext, Task>, EndpointMetadata?)

Maps an endpoint for the given HTTP method.

public void MapMethod(string httpMethod, string pattern, Func<IHttpRequestContext, Task> handler, EndpointMetadata? metadata = null)

Parameters

httpMethod string

The HTTP method (e.g. GET, POST, QUERY) to map.

pattern string

The route pattern.

handler Func<IHttpRequestContext, Task>

The handler for the request.

metadata EndpointMetadata

Optional metadata for the endpoint (tags, name, etc.).

Remarks

This is the general extension point for mapping endpoints — new HTTP methods (such as QUERY) are supported by passing the method token, without changing the callers. The default implementation dispatches GET and POST to MapGet(string, Func<IHttpRequestContext, Task>, EndpointMetadata?) and MapPost(string, Func<IHttpRequestContext, Task>, EndpointMetadata?) and ignores any other method; implementers that support additional verbs (such as QUERY) override this.

MapPost(string, Func<IHttpRequestContext, Task>, EndpointMetadata?)

Maps a POST endpoint.

public void MapPost(string pattern, Func<IHttpRequestContext, Task> handler, EndpointMetadata? metadata = null)

Parameters

pattern string

The route pattern.

handler Func<IHttpRequestContext, Task>

The handler for the request.

metadata EndpointMetadata

Optional metadata for the endpoint (tags, name, etc.).

SetPathBase(string)

Sets the path base for all endpoints.

public void SetPathBase(string pathBase)

Parameters

pathBase string

The base path.

Start(IServiceProvider)

Starts the HTTP listener.

public void Start(IServiceProvider serviceProvider)

Parameters

serviceProvider IServiceProvider

The service provider for request scoping.

Stop()

Stops the HTTP listener.

public Task Stop()

Returns

Task

A task representing the stop operation.

UseStaticFiles(StaticFileOptions)

Configures static file serving with the specified options.

public void UseStaticFiles(StaticFileOptions options)

Parameters

options StaticFileOptions

The options for static file serving.