RandomSkunk.Results.AspNetCore 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package RandomSkunk.Results.AspNetCore --version 1.1.0                
NuGet\Install-Package RandomSkunk.Results.AspNetCore -Version 1.1.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.1.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.1.0                
#r "nuget: RandomSkunk.Results.AspNetCore, 1.1.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.1.0

// Install RandomSkunk.Results.AspNetCore as a Cake Tool
#tool nuget:?package=RandomSkunk.Results.AspNetCore&version=1.1.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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
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.4.0 505 6/28/2023
1.3.1 184 5/15/2023
1.3.0 177 5/3/2023
1.2.0 192 4/11/2023
1.1.0 242 3/28/2023
1.0.0 337 1/12/2023