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
-
objectMvcResultExtensions
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
resultResultThe Result to be converted.
useProblemDetailsboolWhether 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
useProblemDetailsistrue. - Microsoft.AspNetCore.Mvc.NotFoundResult for a PreconditionFailedResult indicating NotFound if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Mvc.StatusCodeResult with HTTP status code 412 for a PreconditionFailedResult indicating ConcurrencyMismatch if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Mvc.ConflictResult for a PreconditionFailedResult indicating Conflict if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Mvc.BadRequestObjectResult with model state populated for a ValidationFailedResult if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Mvc.ForbidResult in case of an UnauthorizedResult if
useProblemDetailsisfalse.
Exceptions
- ArgumentOutOfRangeException
Thrown when the
resultdoes 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
resultResult<T>The result instance to convert.
successMapFunc<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.
failureMapFunc<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.
useProblemDetailsboolWhether 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
successMapis not specified. - The result of
failureMapfor any non-successful result, whenfailureMapis specified. - Microsoft.AspNetCore.Mvc.ObjectResult with Microsoft.AspNetCore.Mvc.ProblemDetails for any non-successful result if
useProblemDetailsistrueandfailureMapis not specified. - Microsoft.AspNetCore.Mvc.NotFoundResult for a PreconditionFailedResult<T> indicating NotFound if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Mvc.StatusCodeResult with HTTP status code 412 for a PreconditionFailedResult<T> indicating ConcurrencyMismatch if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Mvc.ConflictResult for a PreconditionFailedResult<T> indicating Conflict if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Mvc.BadRequestObjectResult with model state populated for a ValidationFailedResult<T> if
useProblemDetailsisfalse. - Microsoft.AspNetCore.Mvc.ForbidResult in case of an UnauthorizedResult<T> if
useProblemDetailsisfalse.
- The result of
Type Parameters
TThe type of the value contained in the result, if successful.
Exceptions
- ArgumentOutOfRangeException
Thrown when the
resultdoes 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
resultResultThe Result to be converted.
statusCodeHttpStatusCodeThe 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.