Hesham.ResultPattern.Extension.MVC 1.1.2

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

ResultPattern.Extension.MVC

MVC & Web API response helpers for ResultPattern.
Maps Result<TValue> and List<Error> directly to standards-compliant HTTP Problem Details ActionResult responses — powered by ASP.NET Core's built-in ProblemDetailsFactory.

This package works with any class that inherits ControllerBase — including [ApiController] Web API controllers and traditional MVC controllers alike.


📦 Installation

dotnet add package ResultPattern.Extension.MVC

Requires: ResultPattern core package.


🚀 Quick Start

1. Register Services

builder.Services.AddResponseHelper();

2. Inject and Use in Controllers

[ApiController]
[Route("api/[controller]")]
public class BasketController : ControllerBase
{
    private readonly ISender _sender;
    private readonly ResponseHelper _responseHelper;

    public BasketController(ISender sender, ResponseHelper responseHelper)
    {
        _sender = sender;
        _responseHelper = responseHelper;
    }

    [HttpGet("{userName}")]
    public async Task<IActionResult> GetBasket(string userName, CancellationToken ct)
    {
        var query = new GetBasketQuery(userName);

        Result<GetBasketResult> result = await _sender.Send(query, ct);

        return result.Match(
            val => Ok(val.Adapt<GetBasketResponse>()),
            _responseHelper.ToProblemResult);
    }
}

ResponseHelper is registered as a scoped service and uses IHttpContextAccessor internally — inject it into any controller that needs result-to-response mapping.


ResponseHelper

The core service exposed by this package. It takes a List<Error> and produces an ActionResult shaped as an RFC 7807 Problem Details response, using ASP.NET Core's ProblemDetailsFactory for full framework compatibility.

ToProblemResult(List<Error> errors)

The main entry point. Inspects the error list and delegates to the appropriate response builder:

Condition Response Type Status Code
All errors are 422 Unprocessable Entity ValidationProblemDetails 422
Mixed or single non-validation error ProblemDetails Error's own HttpStatusCode
Null or empty error list Generic ProblemDetails 500

Response Shapes

Validation Errors — 422 Unprocessable Entity

Triggered when all errors in the list carry a 422 status code (i.e. all created via Error.Validation(...)).
All descriptions are collected, deduplicated, and added to ModelStateDictionary under the "errors" key — fully compatible with ASP.NET Core's standard validation response shape.

{
  "title": "Validation",
  "status": 422,
  "detail": "One or more validation errors occurred.",
  "errors": {
    "errors": [
      "Name cannot be null or empty.",
      "Name cannot be less than 3 characters.",
      "Invalid email address."
    ]
  }
}

In Development, each error's full stack trace is appended under extensions.stackTraces:

{
  "title": "Validation",
  "status": 422,
  "detail": "One or more validation errors occurred.",
  "errors": {
    "errors": [
      "Name cannot be null or empty.",
      "Invalid email address."
    ]
  },
  "extensions": {
    "stackTraces": [
      {
        "description": "Name cannot be null or empty.",
        "stackTrace": {
          "memberName": "Create",
          "fileName": "User.cs",
          "lineNumber": 12
        }
      },
      {
        "description": "Invalid email address.",
        "stackTrace": {
          "memberName": "Create",
          "fileName": "User.cs",
          "lineNumber": 18
        }
      }
    ]
  }
}

Single / Mixed Errors — Problem Details

Triggered when errors are not all 422 — e.g. 404, 403, 409, or 500.
The first error in the list drives the response status and title.

{
  "title": "NotFound",
  "status": 404,
  "detail": "Restaurant with id '5' not found."
}

In Development, the error's stack trace is appended under extensions.stackTrace:

{
  "title": "NotFound",
  "status": 404,
  "detail": "Restaurant with id '5' not found.",
  "extensions": {
    "stackTrace": {
      "memberName": "Handle",
      "fileName": "DeleteRestaurantCommandHandler.cs",
      "lineNumber": 14
    }
  }
}

Stack traces are never exposed in Production. IHostEnvironment is checked internally — no configuration required.


Environment-Aware Diagnostics

ResponseHelper automatically adjusts its response detail based on the current environment:

Field Development Production
extensions.stackTrace ✅ Included ❌ Omitted
extensions.stackTraces ✅ Included ❌ Omitted
Error descriptions ✅ Included ✅ Included
HTTP status codes ✅ Included ✅ Included

Full Controller Example

A complete Web API controller using ResultPattern and ResponseHelper across multiple endpoints:

[ApiController]
[Route("api/[controller]")]
public class RestaurantsController : ControllerBase
{
    private readonly ISender _sender;
    private readonly ResponseHelper _responseHelper;

    public RestaurantsController(ISender sender, ResponseHelper responseHelper)
    {
        _sender = sender;
        _responseHelper = responseHelper;
    }

    [HttpDelete("{id:guid}")]
    public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
    {
        var command = new DeleteRestaurantCommand(id);

        Result<Success> result = await _sender.Send(command, ct);

        return result.Match(
            _ => NoContent(),
            _responseHelper.ToProblemResult);
    }

    [HttpPost]
    public async Task<IActionResult> Create(CreateRestaurantRequest request, CancellationToken ct)
    {
        var command = new CreateRestaurantCommand(request.Name, request.Address);

        Result<RestaurantResponse> result = await _sender.Send(command, ct);

        return result.Match(
            restaurant => CreatedAtAction(nameof(GetById), new { id = restaurant.Id }, restaurant),
            _responseHelper.ToProblemResult);
    }

    [HttpGet("{id:guid}")]
    public async Task<IActionResult> GetById(Guid id, CancellationToken ct)
    {
        var query = new GetRestaurantByIdQuery(id);

        Result<RestaurantResponse> result = await _sender.Send(query, ct);

        return result.Match(
            restaurant => Ok(restaurant),
            _responseHelper.ToProblemResult);
    }
}

MVC & Web API vs Minimal API

Both extension packages expose an identical ResponseHelper API surface and produce the same Problem Details response shapes. Choose based on how your application is structured:

ResultPattern.Extension.MVC ResultPattern.Extension.MinimalAPI
Return type ActionResult IResult
Factory ProblemDetailsFactory Results.Problem / Results.ValidationProblem
HttpContext source IHttpContextAccessor Endpoint delegate parameter
Use in ControllerBase subclasses (MVC & Web API) app.Map* endpoint delegates
Stack trace in Dev ✅ Yes ✅ Yes

Package Purpose
ResultPattern Core Result<T>, Error, Ensure, Combine
ResultPattern.Extension.MinimalAPI HTTP Problem Details mapping for Minimal APIs
ResultPattern.Extension.MVC This package — HTTP Problem Details mapping for MVC & Web API Controllers
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
1.1.2 86 3/1/2026