-
Notifications
You must be signed in to change notification settings - Fork 1
ResultObjects (AspNetCore extension)
CloudFy edited this page Sep 26, 2024
·
5 revisions
This extension simplifies handling return of result objects, by encapsulating result as standard response models for both ASP.NET Core MVC (IActionResult) or minimal API (IResult) architectures.
See ResultObject for initial introduction.
dotnet add package Arcturus.Extensions.ResultObjects.AspNetCore The package is dependant on ProblemDetailsFactory to function.
Either 1) register this using AddProblemDetails in your startup
// register ProblemDetailsFactory
builder.Services.AddProblemDetails();or 2) register this using our API
using Arcturus.Extensions.ResultObjects.AspNetCore;
...
// registers dependencies
builder.Services.UseResultObjects();The packages includes two methods. ToActionResult for ASP.NET MVC or ToResult to minimal API designs.
using Arcturus.ResultObjects;
...
// sample returning a failed result using ASP.NET Core MVC
[HttpGet("")]
public IActionResult GetFail()
{
return Result
.Failure<SampleItem>(new Fault("", ""))
.WithHttpStatusCode(System.Net.HttpStatusCode.BadRequest)
.ToActionResult();
}Both methods handle the result mappping, providing a ProblemDetails response for anything not (Result.IsSuccess = true).
Results must be handled with proper status code mapping.
- (Result.IsSuccess = true) will return HTTP 200 (OK). The
.Valueis passed as response body. - (Result.IsSuccess = false) with a HTTP status code from
WithHttpStatusCode(...)sets the status code as such. - Any result other than this, is providing a HTTP 500 (internal server error).