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
-
objectMinimalApiResultExtensions
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
resultResultThe Result to be converted.
useProblemDetailsboolWhether 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
useProblemDetailsistrue. - Microsoft.AspNetCore.Http.HttpResults.NotFound for a PreconditionFailedResult with a reason of NotFound if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Http.HttpResults.StatusCodeHttpResult with status code 412 for a PreconditionFailedResult with a reason of ConcurrencyMismatch if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Http.HttpResults.Conflict for a PreconditionFailedResult with a reason of Conflict if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Http.HttpResults.BadRequest with model state populated for a ValidationFailedResult if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Http.HttpResults.ForbidHttpResult for an UnauthorizedResult if
useProblemDetailsisfalse.
Exceptions
- ArgumentOutOfRangeException
Thrown when the
resultdoes 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
resultResult<T>The Result<T> instance to be converted.
successMapFunc<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.
useProblemDetailsboolWhether 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
successMapis not provided. - The result of
successMapif 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
useProblemDetailsistrue. - Microsoft.AspNetCore.Http.HttpResults.NotFound for a PreconditionFailedResult<T> with a reason of NotFound if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Http.HttpResults.StatusCodeHttpResult with status code 412 for a PreconditionFailedResult<T> with a reason of ConcurrencyMismatch if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Http.HttpResults.Conflict for a PreconditionFailedResult<T> with a reason of Conflict if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Http.HttpResults.BadRequest with model state populated for a ValidationFailedResult<T> if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Http.HttpResults.ForbidHttpResult for an UnauthorizedResult<T> if
useProblemDetailsisfalse.
- Microsoft.AspNetCore.Http.HttpResults.Ok response for a SuccessResult<T> if
Type Parameters
TThe type of the result's value.
Exceptions
- ArgumentOutOfRangeException
Thrown when the
resultdoes 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
resultResultThe Result to be converted.
statusCodeHttpStatusCodeThe 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.