RGamaFelix.ServiceResponse.RestResponse 3.1.0

dotnet add package RGamaFelix.ServiceResponse.RestResponse --version 3.1.0
                    
NuGet\Install-Package RGamaFelix.ServiceResponse.RestResponse -Version 3.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="RGamaFelix.ServiceResponse.RestResponse" Version="3.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RGamaFelix.ServiceResponse.RestResponse" Version="3.1.0" />
                    
Directory.Packages.props
<PackageReference Include="RGamaFelix.ServiceResponse.RestResponse" />
                    
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 RGamaFelix.ServiceResponse.RestResponse --version 3.1.0
                    
#r "nuget: RGamaFelix.ServiceResponse.RestResponse, 3.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.
#:package RGamaFelix.ServiceResponse.RestResponse@3.1.0
                    
#: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=RGamaFelix.ServiceResponse.RestResponse&version=3.1.0
                    
Install as a Cake Addin
#tool nuget:?package=RGamaFelix.ServiceResponse.RestResponse&version=3.1.0
                    
Install as a Cake Tool

RGamaFelix.ServiceResponse.RestResponse

ASP.NET Core integration package that provides automatic HTTP response mapping for ServiceResponse results.

Overview

This package extends the RGamaFelix.ServiceResponse library with seamless ASP.NET Core integration, automatically converting service result types into appropriate HTTP status codes and action results. Perfect for building RESTful APIs with consistent error handling and response formatting.

Installation

  dotnet add package RGamaFelix.ServiceResponse.RestResponse

Dependencies

  • Microsoft.AspNetCore.Mvc.Core (2.3.0+)
  • Microsoft.AspNetCore.Mvc.Abstractions (2.3.0+)
  • RGamaFelix.ServiceResponse (3.0.0+)
  • .NET 9.0+

Features

  • ✅ Automatic HTTP mapping from ResultTypeCode to HTTP status codes
  • ✅ Extension method integration with fluent API design
  • ✅ Smart Created result handling with optional location headers
  • ✅ Consistent error responses with structured error data
  • ✅ Type-safe conversions maintaining data integrity
  • ✅ Zero configuration - works out of the box

HTTP Status Code Mappings

ResultTypeCode HTTP Status Action Result Use Case
Ok 200 OK OkObjectResult General successful operation
Found 200 OK OkObjectResult Resource successfully found
Created 201 Created CreatedResult / ObjectResult Resource successfully created
InvalidData 400 Bad Request BadRequestObjectResult Validation or data format errors
NotFound 404 Not Found NotFoundObjectResult Requested resource not found
AuthenticationError 401 Unauthorized UnauthorizedResult User not authenticated
AuthorizationError 403 Forbidden ForbidResult User not authorized
Multiplicity 409 Conflict ConflictObjectResult Multiple resources when one expected
GenericError 500 Internal Server Error ObjectResult General error condition
UnexpectedError 500 Internal Server Error ObjectResult Unexpected exceptions

Usage

Basic Controller Integration

using RGamaFelix.ServiceResponse.RestResponse;
using Microsoft.AspNetCore.Mvc;

[ApiController] 
[Route("api/[controller]")] 
public class UsersController : ControllerBase
{
    private readonly IUserService _userService;
    public UsersController(IUserService userService)
    {
        _userService = userService;
    }
    
    [HttpGet("{id}")]
    public IActionResult GetUser(int id)
    {
        var result = _userService.GetUser(id);
        return result.ReturnServiceResult();
        
        // Automatically handles:
        // - Success: 200 OK with user data
        // - Not Found: 404 Not Found with error messages
        // - Validation Error: 400 Bad Request with error details
    }

    [HttpPost]
    public IActionResult CreateUser(CreateUserRequest request)
    {
        var result = _userService.CreateUser(request);
        return result.ReturnServiceResult($"/api/users/{result.Data?.Id}");
        
        // Automatically handles:
        // - Success: 201 Created with Location header
        // - Validation Error: 400 Bad Request
        // - Conflict: 409 Conflict
    }
    
    [HttpPut("{id}")]
    public IActionResult UpdateUser(int id, UpdateUserRequest request)
    {
        var result = _userService.UpdateUser(id, request);
        return result.ReturnServiceResult();
        // Automatically handles all result types
    }

    [HttpDelete("{id}")]
       public IActionResult DeleteUser(int id)
    {
        var result = _userService.DeleteUser(id);
        return result.ReturnServiceResult();
    }
}

Advanced Usage with Custom Error Handling

[HttpPost]
public IActionResult CreateOrder(CreateOrderRequest request)
{
    var result = _orderService.CreateOrder(request);
    // The extension method handles all mapping automatically
    return result.ReturnServiceResult($"/api/orders/{result.Data?.Id}");        

    // Result mapping examples:
    // - ResultTypeCode.Created -> 201 Created with Location header
    // - ResultTypeCode.InvalidData -> 400 Bad Request with validation errors
    // - ResultTypeCode.AuthorizationError -> 403 Forbidden
    // - ResultTypeCode.UnexpectedError -> 500 Internal Server Error
}

Error Response Format

Error responses automatically include structured error information:

// 400 Bad Request example 
{
  "errors": "Invalid email address.",
  "details": "The email address is invalid."
}
// 404 Not Found example 
{
  "errors": "Requested user not found."
}
// 409 Conflict example 
{
  "errors": "User already exists."
}
// 500 Internal Server Error example 
{
  "errors": "An unexpected error occurred.
}

Success Response Format

Success responses include the actual data:

 // 200 OK example 
{
  "id": 123,
  "name": "John Doe",
  "email": "john@example.com",
  "createdAt": "2023-12-01T10:00:00Z"
}
// 201 Created example (with Location header)
{
  "id": 124,
  "name": "Jane Doe",
  "email": "jane@example.com",
  "createdAt": "2023-12-01T10:05:00Z"
}

API Reference

Extension Methods

ReturnServiceResult<T>(IServiceResultOf<T>, string?)

Converts a service result into an appropriate HTTP action result.

Parameters:

  • response - The service result to convert (required)
  • uri - Optional URI for Created responses (used when ResultTypeCode is Created)

Returns:

  • IActionResult - Appropriate ASP.NET Core action result with correct HTTP status code

Example:

var serviceResult = _userService.GetUser(id);
return serviceResult.ReturnServiceResult();

Created Result Behavior

When ResultTypeCode.Created is returned:

  • With URI parameter: Returns CreatedResult with Location header
  • Without URI parameter: Returns ObjectResult with 201 status code
// With location header
return result.ReturnServiceResult("/api/users/123");
// Without location header
return result.ReturnServiceResult();
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 is compatible.  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 is compatible.  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
3.1.0 140 5/31/2026
3.0.0 118 5/5/2026
2.0.1 252 9/21/2025
2.0.0 244 6/6/2024
1.0.0 275 1/8/2024