Table of Contents

Class MvcResultExtensions

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

Provides extension methods for converting Result objects to ASP.NET Core MVC Microsoft.AspNetCore.Mvc.IActionResult objects.

public static class MvcResultExtensions
Inheritance
object
MvcResultExtensions

Methods

ToActionResult(Result, bool)

Converts a Result into an appropriate Microsoft.AspNetCore.Mvc.IActionResult that represents the result to be sent in an HTTP response.

public static IActionResult ToActionResult(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

IActionResult

An Microsoft.AspNetCore.Mvc.IActionResult representing the HTTP response:

  • Microsoft.AspNetCore.Mvc.NoContentResult for SuccessResult.
  • Microsoft.AspNetCore.Mvc.ObjectResult with Microsoft.AspNetCore.Mvc.ProblemDetails for any non-successful result if useProblemDetails is true.
  • Microsoft.AspNetCore.Mvc.NotFoundResult for a PreconditionFailedResult indicating NotFound if useProblemDetails is false.
  • Microsoft.AspNetCore.Mvc.StatusCodeResult with HTTP status code 412 for a PreconditionFailedResult indicating ConcurrencyMismatch if useProblemDetails is false.
  • Microsoft.AspNetCore.Mvc.ConflictResult for a PreconditionFailedResult indicating Conflict if useProblemDetails is false.
  • Microsoft.AspNetCore.Mvc.BadRequestObjectResult with model state populated for a ValidationFailedResult if useProblemDetails is false.
  • Microsoft.AspNetCore.Mvc.ForbidResult in case of an UnauthorizedResult if useProblemDetails is false.

Exceptions

ArgumentOutOfRangeException

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

ToActionResult<T>(Result<T>, Func<T, IActionResult>?, Func<Result<T>, IActionResult>?, bool)

Converts a Result<T> into an appropriate Microsoft.AspNetCore.Mvc.IActionResult that represents the result to be sent in an HTTP response.

public static IActionResult ToActionResult<T>(this Result<T> result, Func<T, IActionResult>? successMap = null, Func<Result<T>, IActionResult>? failureMap = null, bool useProblemDetails = true)

Parameters

result Result<T>

The result instance to convert.

successMap Func<T, IActionResult>

An optional function to map a successful result to a custom Microsoft.AspNetCore.Mvc.IActionResult. If not provided, a default mapping is applied.

failureMap Func<Result<T>, IActionResult>

An optional function to map a failed result to a custom Microsoft.AspNetCore.Mvc.IActionResult. If provided, it is used for all non-success results instead of the default mappings.

useProblemDetails bool

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

Returns

IActionResult

An Microsoft.AspNetCore.Mvc.IActionResult representing the HTTP response:

  • The result of successMap, if specified, for a SuccessResult<T>.
  • Microsoft.AspNetCore.Mvc.OkObjectResult for a SuccessResult<T>, when successMap is not specified.
  • The result of failureMap for any non-successful result, when failureMap is specified.
  • Microsoft.AspNetCore.Mvc.ObjectResult with Microsoft.AspNetCore.Mvc.ProblemDetails for any non-successful result if useProblemDetails is true and failureMap is not specified.
  • Microsoft.AspNetCore.Mvc.NotFoundResult for a PreconditionFailedResult<T> indicating NotFound if useProblemDetails is false.
  • Microsoft.AspNetCore.Mvc.StatusCodeResult with HTTP status code 412 for a PreconditionFailedResult<T> indicating ConcurrencyMismatch if useProblemDetails is false.
  • Microsoft.AspNetCore.Mvc.ConflictResult for a PreconditionFailedResult<T> indicating Conflict if useProblemDetails is false.
  • Microsoft.AspNetCore.Mvc.BadRequestObjectResult with model state populated for a ValidationFailedResult<T> if useProblemDetails is false.
  • Microsoft.AspNetCore.Mvc.ForbidResult in case of an UnauthorizedResult<T> if useProblemDetails is false.

Type Parameters

T

The type of the value contained in the result, if successful.

Exceptions

ArgumentOutOfRangeException

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

ToProblemDetailsResult(Result, HttpStatusCode)

Converts a Result into a Microsoft.AspNetCore.Mvc.ObjectResult containing Microsoft.AspNetCore.Mvc.ProblemDetails.

public static IActionResult ToProblemDetailsResult(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

IActionResult

A Microsoft.AspNetCore.Mvc.ObjectResult containing the Microsoft.AspNetCore.Mvc.ProblemDetails.

Remarks

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