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.