Go to file
2026-03-08 20:03:00 +01:00
assets/badges (docs): coverage badges updated 2026-03-08 20:03:00 +01:00
src (bugfix): objectresult uses app json options and handles null request services; add tests, reorg test layout, update readme changelog contributing, bump to 2.0.1 2026-03-08 20:00:01 +01:00
utils (bugfix): repo utils updater 2026-03-01 13:40:20 +01:00
.gitattributes (feature): init 2024-08-25 04:05:09 +02:00
.gitignore (feature): init 2024-08-30 23:05:05 +02:00
CHANGELOG.md (bugfix): objectresult uses app json options and handles null request services; add tests, reorg test layout, update readme changelog contributing, bump to 2.0.1 2026-03-08 20:00:01 +01:00
CONTRIBUTING.md (bugfix): objectresult uses app json options and handles null request services; add tests, reorg test layout, update readme changelog contributing, bump to 2.0.1 2026-03-08 20:00:01 +01:00
LICENSE.md (chore): migrate to .slnx and refine release scripts/docs 2026-02-27 20:27:01 +01:00
README.md (bugfix): objectresult uses app json options and handles null request services; add tests, reorg test layout, update readme changelog contributing, bump to 2.0.1 2026-03-08 20:00:01 +01:00

MaksIT.Results

Line Coverage Branch Coverage Method Coverage

MaksIT.Results is a .NET library for modeling operation outcomes as HTTP-aware result objects and converting them to IActionResult in ASP.NET Core.

Features

  • Result and Result<T> models with status code, success flag, and messages.
  • Static factory methods for common and extended HTTP status codes (1xx, 2xx, 3xx, 4xx, 5xx).
  • Built-in conversion to IActionResult via ToActionResult().
  • RFC 7807-style error payloads for failures (application/problem+json).
  • Camel-case JSON serialization for response bodies; respects app-configured JsonSerializerOptions (e.g. AddJsonOptions with DefaultIgnoreCondition.WhenWritingNull).

Installation

Package Manager:

Install-Package MaksIT.Results

dotnet CLI:

dotnet add package MaksIT.Results

Target Framework

  • .NET 10 (net10.0)

Quick Start

Create results

using MaksIT.Results;

Result ok = Result.Ok("Operation completed");
Result failed = Result.BadRequest("Validation failed");

Result<int> okWithValue = Result<int>.Ok(42, "Answer generated");
Result<string?> notFound = Result<string?>.NotFound(null, "Entity not found");

Convert between result types

using MaksIT.Results;

Result<int> source = Result<int>.Ok(42, "Value loaded");

// Result<T> -> Result<U>
Result<string?> mapped = source.ToResultOfType(v => v?.ToString());

// Result<T> -> Result
Result nonGeneric = source.ToResult();

Use in an ASP.NET Core controller

using MaksIT.Results;
using Microsoft.AspNetCore.Mvc;

public sealed class UsersController : ControllerBase {
  [HttpGet("{id:guid}")]
  public IActionResult GetUser(Guid id) {
    Result<UserDto?> result = id == Guid.Empty
      ? Result<UserDto?>.BadRequest(null, "Invalid id")
      : Result<UserDto?>.Ok(new UserDto(id, "maks"), "User loaded");

    return result.ToActionResult();
  }
}

public sealed record UserDto(Guid Id, string Name);

ToActionResult() Behavior

  • Result success: returns status-code-only response.
  • Result<T> success with non-null Value: returns JSON body + status code.
  • Any failure: returns RFC 7807-style ProblemDetails JSON with:
    • status = result status code
    • title = "An error occurred"
    • detail = joined Messages
    • content type application/problem+json

JSON options

ObjectResult uses the same JsonSerializerOptions as your app when you configure them with AddJsonOptions:

builder.Services.AddControllers()
  .AddJsonOptions(options => {
    options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
  });

If no options are registered, a default (camel-case) serializer is used.

Status Code Factories

  • Informational: Result.Continue(...), Result.SwitchingProtocols(...), Result.Processing(...), etc.
  • Success: Result.Ok(...), Result.Created(...), Result.NoContent(...), etc.
  • Redirection: Result.Found(...), Result.PermanentRedirect(...), etc.
  • Client error: Result.BadRequest(...), Result.NotFound(...), Result.TooManyRequests(...), etc.
  • Server error: Result.InternalServerError(...), Result.ServiceUnavailable(...), etc.

Generic equivalents are available via Result<T>, for example Result<MyDto>.Ok(value, "message").

Contributing

See CONTRIBUTING.md.

Contact

If you have any questions or need further assistance, feel free to reach out:

License

See LICENSE.md.