RandomSkunk.Results.AspNetCore 1.0.0

.NET 6.0
dotnet add package RandomSkunk.Results.AspNetCore --version 1.0.0
NuGet\Install-Package RandomSkunk.Results.AspNetCore -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RandomSkunk.Results.AspNetCore" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RandomSkunk.Results.AspNetCore --version 1.0.0
#r "nuget: RandomSkunk.Results.AspNetCore, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RandomSkunk.Results.AspNetCore as a Cake Addin
#addin nuget:?package=RandomSkunk.Results.AspNetCore&version=1.0.0

// Install RandomSkunk.Results.AspNetCore as a Cake Tool
#tool nuget:?package=RandomSkunk.Results.AspNetCore&version=1.0.0

RandomSkunk.Results.AspNetCore NuGet

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
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 149 1/12/2023
1.0.0-alpha23 57 1/3/2023
1.0.0-alpha22 53 12/9/2022
1.0.0-alpha21 63 11/10/2022
1.0.0-alpha20 69 10/26/2022
1.0.0-alpha19 58 10/18/2022
1.0.0-alpha18 57 10/13/2022
1.0.0-alpha17 57 10/3/2022
1.0.0-alpha16 79 9/22/2022
1.0.0-alpha15 72 7/29/2022
1.0.0-alpha14 77 7/1/2022
1.0.0-alpha13 83 6/3/2022
1.0.0-alpha12 78 6/1/2022
1.0.0-alpha11 77 5/26/2022
1.0.0-alpha10 75 5/24/2022
1.0.0-alpha09 81 5/19/2022
1.0.0-alpha08 79 5/12/2022
1.0.0-alpha07 78 5/10/2022
1.0.0-alpha06 78 5/7/2022
1.0.0-alpha05 84 5/5/2022
1.0.0-alpha04 82 5/3/2022