Knight.Response.Mvc 2.0.0-preview03

This is a prerelease version of Knight.Response.Mvc.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Knight.Response.Mvc --version 2.0.0-preview03
                    
NuGet\Install-Package Knight.Response.Mvc -Version 2.0.0-preview03
                    
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="Knight.Response.Mvc" Version="2.0.0-preview03" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Knight.Response.Mvc" Version="2.0.0-preview03" />
                    
Directory.Packages.props
<PackageReference Include="Knight.Response.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 Knight.Response.Mvc --version 2.0.0-preview03
                    
#r "nuget: Knight.Response.Mvc, 2.0.0-preview03"
                    
#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 Knight.Response.Mvc@2.0.0-preview03
                    
#: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=Knight.Response.Mvc&version=2.0.0-preview03&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Knight.Response.Mvc&version=2.0.0-preview03&prerelease
                    
Install as a Cake Tool

Knight.Response.Mvc

Knight.Response.Mvc provides integration between Knight.Response and ASP.NET MVC / Web API 2 (System.Web on .NET Framework 4.7.1+). It ensures consistent API response handling with standardized result-to-HTTP mapping, including ProblemDetails compatibility, validation error shaping, and factory methods.

This package is the MVC counterpart to Knight.Response.AspNetCore, designed for projects that cannot use ASP.NET Core but still want the same structured response model.

NuGet Version ci Mutation score


Features

  • Extension Methods

    • ResultExtensions → Converts Result / Result<T> into IActionResult
    • ServiceCollectionExtensions → Registers Knight.Response services for MVC
  • Factories

    • ApiResults → Strongly-typed helpers to return success, failure, unauthorized, forbidden, etc.
    • ProblemFactory → Builds RFC 7807-compatible ProblemDetails for error responses
  • Infrastructure

    • CompatProblemDetails → Compatible ProblemDetails implementation for MVC / Web API 2
    • CompatValidationProblemDetails → Validation error details aligned with modern ASP.NET Core behavior
  • Options

    • KnightResponseOptions → Configurable options for customizing error shape, problem details, status mapping, etc.
    • ResultHttpResolver → Centralises Result.Status and ResultCode → HTTP status resolution

Installation

dotnet add package Knight.Response.Mvc --version 2.0.0-preview03

This package depends on:

  • Knight.Response (core results)
  • Knight.Response.Abstractions.Http (shared HTTP abstractions: options, resolver, validation mapper)
  • Microsoft.AspNetCore.Mvc (2.2.0) — referenced for ProblemDetails, ValidationProblemDetails, and schema alignment.

Note: This does not require ASP.NET Core runtime. Types are used for schema compatibility only.


Usage

1. Configure Services

Register Knight.Response in Startup.cs:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var services = new ServiceCollection();
        services.AddKnightResponse(); // from ServiceCollectionExtensions
    }
}

2. Return Results in Controllers

public class AccountsController : ApiController
{
    [HttpGet]
    public IHttpActionResult GetAccount(string id)
    {
        var result = _accountService.GetById(id);

        // Convert Result<T> → IActionResult
        return result.ToOkActionResult();
    }

    [HttpPost]
    public IHttpActionResult Create(CreateAccountRequest request)
    {
        var result = _accountService.Create(request);

        // Returns standardized 201 Created or BadRequest/ProblemDetails as per configuration
        return result.ToCreatedActionResult();
    }
}

Success Payload Shape

Controlled by KnightResponseOptions.IncludeFullResultPayload:

  • false (default since 2.0.0-preview01):

    • Ok → returns Value (of Result<T>) only
    • Created / Accepted → Value only
  • true: return the full Result object on success (useful when clients want codes/messages on success).

Failure Mapping

  • If UseValidationProblemDetails is true and the mapper produces field errors → response is ValidationProblemDetails.

  • Otherwise → response is ProblemDetails.

  • Status code resolution order:

    1. CodeToHttp (domain ResultCode → HTTP status)
    2. StatusCodeResolver (default mapping: Failed=400, Cancelled=409, Error=500, Completed not used)

Options

Options type comes from:

public sealed class KnightResponseOptions
    : KnightResponseBaseOptions<HttpContext, ProblemDetails, ValidationProblemDetails>
{
    // Core properties:
    // - IncludeFullResultPayload (default: false)
    // - UseProblemDetails (default: false)
    // - UseValidationProblemDetails (default: false)
    // - IncludeExceptionDetails (default: false)
    // - CodeToHttp (optional)
    // - StatusCodeResolver (default mapping provided)
    // - ValidationMapper (optional override)
    // - ProblemDetailsBuilder, ValidationBuilder (hooks)
}

Mapper Resolution

At runtime, the validation error mapper is resolved in this order:

  1. From the current request’s DI scope (HttpContext.RequestServices).
  2. From the ValidationMapper override on options.
  3. If neither is set, DefaultValidationErrorMapper is used.

Which package do I use?

  • Use Knight.Response.Mvc → For System.Web MVC / Web API 2 apps targeting .NET Framework.
  • Use Knight.Response.AspNetCore → For ASP.NET Core apps targeting .NET 6/7/8+.

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please read CONTRIBUTING.md.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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-preview04 33 9/27/2025
2.0.0-preview03 102 9/25/2025
2.0.0-preview02 109 9/24/2025
2.0.0-preview01 195 9/24/2025
1.0.0 134 9/14/2025
0.1.0 134 9/14/2025

- Now uses ResultHttpResolver (CodeToHttp first, then Status fallback).
           - Honors codes like NoContent/NotFound; upgrades default 200→204 when applicable.
           - ProblemDetails path unchanged; validation mapper resolved per-request.
           - Updated deps: Core 2.0.0-preview04