Monad.NET.AspNetCore 1.1.2

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Monad.NET.AspNetCore --version 1.1.2
                    
NuGet\Install-Package Monad.NET.AspNetCore -Version 1.1.2
                    
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="Monad.NET.AspNetCore" Version="1.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Monad.NET.AspNetCore" Version="1.1.2" />
                    
Directory.Packages.props
<PackageReference Include="Monad.NET.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Monad.NET.AspNetCore --version 1.1.2
                    
#r "nuget: Monad.NET.AspNetCore, 1.1.2"
                    
#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.
#:package Monad.NET.AspNetCore@1.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Monad.NET.AspNetCore&version=1.1.2
                    
Install as a Cake Addin
#tool nuget:?package=Monad.NET.AspNetCore&version=1.1.2
                    
Install as a Cake Tool

Monad.NET.AspNetCore

ASP.NET Core integration for Monad.NET.

Installation

dotnet add package Monad.NET.AspNetCore

Features

  • IActionResult Extensions - Convert monad types directly to HTTP responses
  • Exception Middleware - Catch exceptions and return Result-style responses
  • ValidationProblemDetails Support - Automatic conversion to RFC 7807 format

Quick Start

IActionResult Extensions

Convert monad types directly to HTTP responses in your controllers:

using Monad.NET;
using Monad.NET.AspNetCore;

[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
    private readonly IUserService _userService;

    // Option → 200 OK or 404 Not Found
    [HttpGet("{id}")]
    public IActionResult GetUser(int id)
    {
        return _userService.FindUser(id)
            .ToActionResult("User not found");
    }

    // Result → 200 OK or error status
    [HttpPost]
    public IActionResult CreateUser(CreateUserRequest request)
    {
        return _userService.CreateUser(request)
            .ToActionResult(StatusCodes.Status400BadRequest);
    }

    // Validation → 200 OK or 422 with validation errors
    [HttpPut("{id}")]
    public IActionResult UpdateUser(int id, UpdateUserRequest request)
    {
        return ValidateRequest(request)
            .ToValidationProblemResult();
    }

    // Async support
    [HttpGet("{id}/profile")]
    public async Task<IActionResult> GetUserProfile(int id)
    {
        return await _userService.GetUserProfileAsync(id)
            .ToActionResultAsync();
    }
}

Custom Response Mapping

// Custom mapping for Result
return result.ToActionResult(
    onOk: user => CreatedAtAction(nameof(GetUser), new { id = user.Id }, user),
    onErr: error => error switch
    {
        UserError.NotFound => NotFound(),
        UserError.Duplicate => Conflict(error),
        _ => BadRequest(error)
    }
);

// Custom mapping for Option
return option.ToActionResult(
    onSome: user => Ok(new { User = user, Timestamp = DateTime.UtcNow }),
    onNone: () => NotFound(new { Message = "User not found" })
);

Exception Handling Middleware

Catch unhandled exceptions and return consistent Result-style responses:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// Add early in the pipeline
app.UseResultExceptionHandler(options =>
{
    options.IncludeExceptionDetails = app.Environment.IsDevelopment();
    options.IncludeStackTrace = app.Environment.IsDevelopment();
});

app.MapControllers();
app.Run();

Response format:

{
  "isOk": false,
  "error": {
    "type": "InvalidOperationException",
    "message": "Something went wrong"
  }
}

ValidationProblemDetails Support

Convert validation errors to RFC 7807 format:

// Simple string errors
var validation = Validation<User, string>.Invalid(
    new[] { "Name is required", "Email is invalid" }
);
return validation.ToValidationProblemResult("user");

// Keyed errors (field-specific)
var validation = Validation<User, KeyValuePair<string, string>>.Invalid(
    new[]
    {
        KeyValuePair.Create("name", "Name is required"),
        KeyValuePair.Create("email", "Email is invalid")
    }
);
return validation.ToValidationProblemResult();

Response:

{
  "title": "Validation failed",
  "status": 422,
  "errors": {
    "name": ["Name is required"],
    "email": ["Email is invalid"]
  }
}

Available Extensions

Option Extensions

Method Description
ToActionResult() Some → 200 OK, None → 404
ToActionResult(message) None → 404 with message
ToActionResult(onSome, onNone) Custom mapping
ToActionResultWithProblemDetails() None → ProblemDetails
ToActionResultAsync() Async version

Result Extensions

Method Description
ToActionResult(errorCode) Ok → 200, Err → specified code
ToActionResult(onOk, onErr) Custom mapping
ToActionResultOrNotFound() Err → 404
ToCreatedResult(location) Ok → 201 Created
ToNoContentResult() Ok → 204 No Content
ToActionResultWithProblemDetails() Err → ProblemDetails
ToActionResultAsync() Async versions

Validation Extensions

Method Description
ToActionResult() Valid → 200, Invalid → 422
ToActionResult(onValid, onInvalid) Custom mapping
ToValidationProblemResult() Invalid → ValidationProblemDetails
ToCreatedResult(location) Valid → 201
ToActionResultAsync() Async versions

Either Extensions

Method Description
ToActionResult(leftCode) Right → 200, Left → specified code
ToActionResult(onLeft, onRight) Custom mapping
ToActionResultAsync() Async versions

Try Extensions

Method Description
ToActionResult(includeDetails) Success → 200, Failure → 500
ToActionResult(onSuccess, onFailure) Custom mapping
ToActionResultWithProblemDetails() Failure → ProblemDetails
ToActionResultAsync() Async versions

Requirements

  • .NET 6.0 or later
  • Monad.NET (automatically referenced)

License

MIT — Free for commercial and personal use.

Author: Behrang Mohseni

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
2.0.0-beta.2 41 2/2/2026
2.0.0-beta.1 38 2/1/2026
1.1.2 91 1/25/2026
1.1.1 90 1/7/2026
1.1.0 96 12/30/2025
1.0.0 91 12/28/2025
1.0.0-beta.2 131 12/24/2025
1.0.0-beta.1 129 12/23/2025
1.0.0-alpha.13 129 12/22/2025
1.0.0-alpha.11 76 12/21/2025
1.0.0-alpha.10 221 12/16/2025
1.0.0-alpha.9 224 12/16/2025
1.0.0-alpha.8 196 12/15/2025
1.0.0-alpha.7 179 12/15/2025