BPITS.Results.AspNetCore 2.0.2

dotnet add package BPITS.Results.AspNetCore --version 2.0.2
                    
NuGet\Install-Package BPITS.Results.AspNetCore -Version 2.0.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="BPITS.Results.AspNetCore" Version="2.0.2">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BPITS.Results.AspNetCore" Version="2.0.2" />
                    
Directory.Packages.props
<PackageReference Include="BPITS.Results.AspNetCore">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 BPITS.Results.AspNetCore --version 2.0.2
                    
#r "nuget: BPITS.Results.AspNetCore, 2.0.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 BPITS.Results.AspNetCore@2.0.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=BPITS.Results.AspNetCore&version=2.0.2
                    
Install as a Cake Addin
#tool nuget:?package=BPITS.Results.AspNetCore&version=2.0.2
                    
Install as a Cake Tool

BPITS.Results.AspNetCore

ASP.NET Core extensions for BPITS.Results that enable ApiResult to implement IActionResult with automatic HTTP status code mapping.

Features

  • Direct IActionResult Implementation - ApiResult directly implements IActionResult and can be returned from ASP.NET Core controllers
  • Attribute-Based HTTP Status Code Mapping - Define HTTP status codes using attributes on your enum values
  • Source Generated - Zero runtime overhead with compile-time code generation
  • Simple Setup - Three-step configuration with automatic mapper registration

Installation

dotnet add package BPITS.Results
dotnet add package BPITS.Results.AspNetCore

Quick Start

1. Define Your Status Enum with ASP.NET Core Attributes

using System.Net;
using BPITS.Results.Abstractions;
using BPITS.Results.AspNetCore.Abstractions;

[GenerateApiResult]
[GenerateServiceResult]
[EnableApiResultMapping]
public enum MyApiStatus
{
    [HttpStatusCode(HttpStatusCode.OK)]
    Ok = 0,

    [HttpStatusCode(HttpStatusCode.BadRequest)]
    BadRequest = 400,

    [HttpStatusCode(HttpStatusCode.NotFound)]
    NotFound = 404,

    [HttpStatusCode(HttpStatusCode.InternalServerError)]
    InternalServerError = 500
}

2. Register the Mapper in Program.cs

var builder = WebApplication.CreateBuilder(args);

// Register the generated mapper
builder.Services.AddMyApiStatusActionResultMapper();

var app = builder.Build();
app.MapControllers();
app.Run();

3. Return ApiResult from Controllers

[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
    [HttpGet("{id}")]
    public ApiResult<UserDto> GetUser(int id)
    {
        var result = _userService.GetUser(id);
        return ApiResult.FromServiceResult(result.MapValue(u => u?.ToDto()));
        // Automatically returns correct HTTP status code
    }
}

How It Works

When you apply [EnableApiResultMapping] to your enum, the source generator:

  1. Generates a partial ApiResult<TEnum> class implementing IActionResult
  2. Creates an HTTP status code mapper based on [HttpStatusCode] attributes
  3. Provides a DI extension method to register the mapper
  4. Automatically maps your custom status codes to HTTP status codes

Why Use Custom Status Codes?

Custom status codes enable consuming applications to handle errors programmatically:

  • Programmatic error handling - Clients switch on status codes to implement appropriate logic (retry, redirect, fallback)
  • No string parsing - Handle errors based on enum values, not parsing error messages
  • Domain-driven - Error codes match your business domain
  • Consistent semantics - Same codes across backend and frontend
  • Flexibility - Change HTTP mapping without affecting client logic
  • Better UX - Proper error handling enables appropriate user experiences

Example Response

Success (HTTP 200)

{
  "value": { "id": 123, "name": "John" },
  "isSuccess": true,
  "statusCode": 0
}

Failure (HTTP 404)

{
  "value": null,
  "isSuccess": false,
  "statusCode": 404,
  "errorMessage": "User not found"
}

Documentation

For complete setup and usage instructions:

Core Package

This package extends BPITS.Results. See the core package for information about ServiceResult and ApiResult fundamentals.

Repository

GitHub Repository

License

MIT License - see the LICENSE file for details.

There are no supported framework assets in this 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.2 157 3/1/2026
2.0.1 782 1/27/2026
2.0.0 123 1/27/2026