Table of Contents

Class MinimalApiResultExtensions

Namespace
SEBT.Portal.Kernel.AspNetCore
Assembly
SEBT.Portal.Kernel.AspNetCore.dll

Provides extension methods for converting Result objects to ASP.NET Core Minimal API Microsoft.AspNetCore.Http.IResult objects.

public static class MinimalApiResultExtensions
Inheritance
object
MinimalApiResultExtensions

Methods

ToMinimalApiResult(Result, bool)

Converts a Result into an appropriate Microsoft.AspNetCore.Http.IResult to be used in a minimal API context.

public static IResult ToMinimalApiResult(this Result result, bool useProblemDetails = true)

Parameters

result Result

The Result to be converted.

useProblemDetails bool

Whether to use a RFC7807 problem details body for a failure response. The default is true.

Returns

IResult

An Microsoft.AspNetCore.Http.IResult that represents the appropriate response:

  • Microsoft.AspNetCore.Http.HttpResults.NoContent for a SuccessResult.
  • Microsoft.AspNetCore.Http.HttpResults.ProblemHttpResult with RFC7807 Microsoft.AspNetCore.Mvc.ProblemDetails for any non-successful result if useProblemDetails is true.
  • Microsoft.AspNetCore.Http.HttpResults.NotFound for a PreconditionFailedResult with a reason of NotFound if useProblemDetails is false.
  • Microsoft.AspNetCore.Http.HttpResults.StatusCodeHttpResult with status code 412 for a PreconditionFailedResult with a reason of ConcurrencyMismatch if useProblemDetails is false.
  • Microsoft.AspNetCore.Http.HttpResults.Conflict for a PreconditionFailedResult with a reason of Conflict if useProblemDetails is false.
  • Microsoft.AspNetCore.Http.HttpResults.BadRequest with model state populated for a ValidationFailedResult if useProblemDetails is false.
  • Microsoft.AspNetCore.Http.HttpResults.ForbidHttpResult for an UnauthorizedResult if useProblemDetails is false.

Exceptions

ArgumentOutOfRangeException

Thrown when the result does not match any known result types.

ToMinimalApiResult<T>(Result<T>, Func<T, IResult>?, bool)

Converts a Result<T> into an appropriate Microsoft.AspNetCore.Http.IResult to be used in a minimal API context.

public static IResult ToMinimalApiResult<T>(this Result<T> result, Func<T, IResult>? successMap = null, bool useProblemDetails = true)

Parameters

result Result<T>

The Result<T> instance to be converted.

successMap Func<T, IResult>

An optional function to map the value of a successful result into a user-defined Microsoft.AspNetCore.Http.IResult. If not provided, successful results will default to an HTTP 200 response with the value serialized as the body.

useProblemDetails bool

Whether to use a RFC7807 problem details body for a failure response. The default is true.

Returns

IResult

An Microsoft.AspNetCore.Http.IResult that represents the appropriate response:

  • Microsoft.AspNetCore.Http.HttpResults.Ok response for a SuccessResult<T> if successMap is not provided.
  • The result of successMap if provided and the result is a SuccessResult<T>.
  • Microsoft.AspNetCore.Http.HttpResults.ProblemHttpResult with RFC7807 Microsoft.AspNetCore.Mvc.ProblemDetails for any non-successful result if useProblemDetails is true.
  • Microsoft.AspNetCore.Http.HttpResults.NotFound for a PreconditionFailedResult<T> with a reason of NotFound if useProblemDetails is false.
  • Microsoft.AspNetCore.Http.HttpResults.StatusCodeHttpResult with status code 412 for a PreconditionFailedResult<T> with a reason of ConcurrencyMismatch if useProblemDetails is false.
  • Microsoft.AspNetCore.Http.HttpResults.Conflict for a PreconditionFailedResult<T> with a reason of Conflict if useProblemDetails is false.
  • Microsoft.AspNetCore.Http.HttpResults.BadRequest with model state populated for a ValidationFailedResult<T> if useProblemDetails is false.
  • Microsoft.AspNetCore.Http.HttpResults.ForbidHttpResult for an UnauthorizedResult<T> if useProblemDetails is false.

Type Parameters

T

The type of the result's value.

Exceptions

ArgumentOutOfRangeException

Thrown when the result does not match any known result types.

ToProblemHttpResult(Result, HttpStatusCode)

Converts a Result into an Microsoft.AspNetCore.Http.HttpResults.ProblemHttpResult containing RFC7807 Microsoft.AspNetCore.Mvc.ProblemDetails.

public static IResult ToProblemHttpResult(this Result result, HttpStatusCode statusCode)

Parameters

result Result

The Result to be converted.

statusCode HttpStatusCode

The HTTP status code to be set in the Microsoft.AspNetCore.Mvc.ProblemDetails.

Returns

IResult

A Microsoft.AspNetCore.Http.HttpResults.ProblemHttpResult containing the Microsoft.AspNetCore.Mvc.ProblemDetails.

Remarks

This is primarily an internal API, intended to be used from ToMinimalApiResult(Result, bool) or ToMinimalApiResult<T>(Result<T>, Func<T, IResult>?, bool). However, it might have some desired use cases where direct usage is appropriate, so it is made public.