Skip to content

Built in support for multiple request payloads

JSON

Example

.Request<CommandDto, Command>

Complete example

public record CommandDto(string Id);

public record Command(string Id);

public class ApiEndpoint : CommandWebApiEndpoint.Request<CommandDto, Command>.Response<FeatureDto, Feature>.Mapper<Mapper, FeatureMapper>
{
    public override Task<Result<Feature>> ExecuteAsync(Command command, CancellationToken cancellationToken) =>
        new Feature($"Name - {command.Id}").ToResultOkAsync();
}

public class Mapper : IWebApiEndpointRequestMapper<CommandDto, Command>
{
    public Result<Command> Map(HttpContext httpContext, CommandDto dto) =>
        new Command(dto.Id).ToResultOk();
}

public class Validator : AbstractValidator<CommandDto>
{
    public Validator()
    {
        RuleFor(x => x.Id).NotEmpty().WithMessage($"must have a value;");
    }
}

Plain text

Example

.RequestPlainText<ApiEndpoint>

Complete example

public class ApiEndpoint : CommandWebApiEndpoint.RequestPlainText<ApiEndpoint>.Response<FeatureDto, Feature>.Mapper<FeatureMapper>
{
    public override Task<Result<Feature>> ExecuteAsync(RequestPlainText query, CancellationToken cancellationToken) =>
        Enumerable.Range(0, 10)
                  .Select(i => new Feature($"Name - {i} - {query.Body}"))
                  .First()
                  .ToResultOkAsync();
}

Upload files

Example

.RequestUploadFiles<ApiEndpoint>

Complete example

public class ApiEndpoint : CommandWebApiEndpoint.RequestUploadFiles<ApiEndpoint>.Response<FeatureDto, Feature>.Mapper<FeatureMapper>
{
    public override Task<Result<Feature>> ExecuteAsync(RequestUploadFiles command, CancellationToken cancellationToken) =>
        Enumerable.Range(0, 10)
                  .Select(i => new Feature($"Name - {i} - {command.Files.Single().FileName}"))
                  .First()
                  .ToResultOkAsync();
}

Upload file

Example

.RequestUploadFile<ApiEndpoint>

Complete example

public class ApiEndpoint : CommandWebApiEndpoint.RequestUploadFile<ApiEndpoint>.Response<FeatureDto, Feature>.Mapper<FeatureMapper>
{
    public override Task<Result<Feature>> ExecuteAsync(RequestUploadFile command, CancellationToken cancellationToken) =>
        Enumerable.Range(0, 10)
                  .Select(i => new Feature($"Name - {i} - {command.File.FileName}"))
                  .First()
                  .ToResultOkAsync();
}

Upload file and json

Example

.RequestUploadFileWithPayload<ApiEndpoint, PayloadDto, Payload>

Complete example

public class ApiEndpoint : CommandWebApiEndpoint.RequestUploadFileWithPayload<ApiEndpoint, PayloadDto, Payload>.Response<FeatureDto, Feature>.Mapper<PayloadMapper, FeatureMapper>
{
    public override Task<Result<Feature>> ExecuteAsync(RequestUploadFileWithPayload<Payload> command, CancellationToken cancellationToken) =>
        Enumerable.Range(0, 10)
                  .Select(i => new Feature($"Name - {command.Payload.Id} - {i} - {command.File.FileName}"))
                  .First()
                  .ToResultOkAsync();
}

Map-From binding

See map-from binding for more information.

Example

public record CommandDto
{
    [MapFromPath("Id")] public string Id { get; set; }
}

Complete example

public record CommandDto
{
    [MapFromPath("Id")] public string Id { get; set; }
}

public record Command(string Id);

public class ApiEndpoint : CommandWebApiEndpoint.Request<CommandDto, Command>.Response<FeatureDto, Feature>
{
    public override Task<Result<Feature>> ExecuteAsync(Command command, CancellationToken cancellationToken) =>
        new Feature($"Name - {command.Id}").ToResultOkAsync();
}

public class Mapper : IWebApiEndpointRequestMapper<CommandDto, Command>
{
    public Result<Command> Map(HttpContext httpContext, CommandDto dto) =>
        new Command(dto.Id).ToResultOk();
}

Map-From-Multipart binding

See map-from-multipart binding for more information.

Example

public record CommandDto
{
    [MapFromMultipartFile(0)] 
    public IFormFile File { get; set; }

    [MapFromMultipartJson(1)] 
    public PayloadDto Payload { get; set; }
}

Complete example

public record CommandDto
{
    [MapFromMultipartFile(0)] 
    public IFormFile File { get; set; }

    [MapFromMultipartJson(1)] 
    public PayloadDto Payload { get; set; }
}

public record PayloadDto(string Id);

public record Request(string Id, IFormFile Files);

public class ApiEndpoint : CommandWebApiEndpoint.Request<CommandDto, Request>.Response<FeatureDto, Feature>.Mapper<Mapper, FeatureMapper>
{
    public override Task<Result<Feature>> ExecuteAsync(Request command, CancellationToken cancellationToken) =>
        Enumerable.Range(0, 10)
                  .Select(i => new Feature($"Name - {command.Id} - {i} - {command.Files.FileName}"))
                  .First()
                  .ToResultOkAsync();
}

public class Mapper : IWebApiEndpointRequestMapper<CommandDto, Request>
{
    public Result<Request> Map(HttpContext httpContext, CommandDto dto) =>
        new Request(dto.Payload.Id, dto.File).ToResultOk();
}

Manual

You can use an instance of 'IWebApiEndpointRequestMapper<TRequestDomain>' to extract information from the 'HttpContext' to create the 'RequestDomain'.

Example

.Request<Query>
.Request<Command>

Complete example

public record Command(Id Id);

public class ApiEndpoint : CommandWebApiEndpoint.Request<Command>.NoResponse
{
    private readonly IBlogStorageBroker _storageBroker;

    public ApiEndpoint(IBlogStorageBroker storageBroker)
    {
        _storageBroker = storageBroker;
    }

    protected override Task<Result> ExecuteAsync(Command command, CancellationToken cancellationToken) =>
        _storageBroker.DeleteAsync(command.Id);
}

public class Mapper : IWebApiEndpointRequestMapper<Command>
{
    public Result<Command> Map(HttpContext httpContext) =>
        httpContext.GetRequestPathParameterAsLong("Id")
                   .Map(id => new Command(id.ToId()));
}