RandomSkunk.Results.AspNetCore
1.3.1
See the version list below for details.
dotnet add package RandomSkunk.Results.AspNetCore --version 1.3.1
NuGet\Install-Package RandomSkunk.Results.AspNetCore -Version 1.3.1
<PackageReference Include="RandomSkunk.Results.AspNetCore" Version="1.3.1" />
paket add RandomSkunk.Results.AspNetCore --version 1.3.1
#r "nuget: RandomSkunk.Results.AspNetCore, 1.3.1"
// Install RandomSkunk.Results.AspNetCore as a Cake Addin #addin nuget:?package=RandomSkunk.Results.AspNetCore&version=1.3.1 // Install RandomSkunk.Results.AspNetCore as a Cake Tool #tool nuget:?package=RandomSkunk.Results.AspNetCore&version=1.3.1
RandomSkunk.Results.AspNetCore
IActionResult extension methods
This library defines extension methods to convert each result type - Result
, Result<T>
, and Maybe<T>
- directly into an equivalent IActionResult
. For Fail
results of any type, the error is converted to a ProblemDetails
object (as described below) and used as the content of the returned action result; its HTTP status code comes from the error's error code. For Success
results of type Result<T>
and Maybe<T>
, the value is used as the content of the returned action result which has a 200 OK
status code. The HTTP status code to use for Success
results can be customized by providing the optional parameter in the ToActionResult()
extension method.
// Result
Result result1 = Result.Success();
result1.ToActionResult(); // 200
Result result2 = Result.Fail("Bad Request", errorCode: 400);
result2.ToActionResult(); // 400 { "status": 400, "title": "Error", "detail": "Bad Request" }
// Result<T>
Result<int[]> result3 = new[] { 1, 2, 3 }.ToResult();
result3.ToActionResult(); // 200 [ 1, 2, 3 ]
Result<int[]> result4 = Result<int[]>.Fail("Forbidden", errorCode: 403);
result4.ToActionResult(); // 403 { "status": 403, "title": "Error", "detail": "Forbidden" }
// Maybe<T>
Maybe<int[]> result5 = new[] { 4, 5, 6 }.ToMaybe();
result5.ToActionResult(); // 200 [ 4, 5, 6 ]
Maybe<int[]> result6 = Maybe<int[]>.None;
result6.ToActionResult(); // 404 { "status": 404, "title": "Error", "detail": "Not Found" }
Maybe<int[]> result7 = Maybe<int[]>.Fail("Not Acceptable", errorCode: 406);
result7.ToActionResult(); // 406 { "status": 406, "title": "Error", "detail": "Not Acceptable" }
GetProblemDetails extension method
The GetProblemDetails
extension method creates a Microsoft.AspNetCore.Mvc.ProblemDetails
object from a RandomSkunk.Results.Error
object. This extension method is called from the IActionResult
extension methods when for Fail
results.
public IActionResult ExampleControllerMethod()
{
Result result = ... // Do some work, returning a Result object
return result.Match(
onSuccess: () => Ok(),
onFail: error =>
{
int errorCode = error.ErrorCode ?? 500;
ProblemDetails problemDetails = error.GetProblemDetails();
return StatusCode(errorCode, problemDetails);
});
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- RandomSkunk.Results (>= 1.3.1)
-
net7.0
- RandomSkunk.Results (>= 1.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.