Skip to content

Latest commit

 

History

History
22 lines (16 loc) · 1.07 KB

File metadata and controls

22 lines (16 loc) · 1.07 KB

Convert Result or Result<TValue> object to Minimal Apis Microsoft.AspNetCore.Http.IResult

ModResults.MinimalApis project contains ToResponse() method implementations to convert Result and Result<TValue> instances in either Ok or Failed state to Microsoft.AspNetCore.Http.IResult.

public record GetBookByIdRequest(Guid Id);

public record GetBookByIdResponse(Guid Id, string Title, string Author, decimal Price);

app.MapPost("GetBookById/{Id}",
    async Task<IResult> (
    [AsParameters] GetBookByIdRequest req,
    [FromServices] IBookService svc,
    CancellationToken cancellationToken) =>
{
    Result<GetBookByIdResponse> result = await svc.GetBookById(req.Id, cancellationToken);
    return result.ToResponse();
}).Produces<GetBookByIdResponse>();

Same project also contains TypedResult extensions to convert failed Result and Result<TValue> instances to ProblemHttpResult object with appropriate HTTP status codes and response formatting.