Knight.Response.Mvc 1.0.0

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Knight.Response.Mvc --version 1.0.0
                    
NuGet\Install-Package Knight.Response.Mvc -Version 1.0.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="Knight.Response.Mvc" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Knight.Response.Mvc" Version="1.0.0" />
                    
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 1.0.0
                    
#r "nuget: Knight.Response.Mvc, 1.0.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 Knight.Response.Mvc@1.0.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=Knight.Response.Mvc&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Knight.Response.Mvc&version=1.0.0
                    
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 brings consistent API response handling, including ProblemDetails compatibility and factory methods for building standardized responses.

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, etc.

Installation

dotnet add package Knight.Response.Mvc

This package depends on:

  • Knight.Response (core results)
  • Knight.Response.Abstractions.Http (shared options + mapper)
  • Microsoft.AspNetCore.Mvc (2.x)

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:

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

Failure mapping

  • If UseValidationProblemDetails is true and the mapper produces fields errors, the response is a ValidationProblemDetails.
  • Otherwise, a standard ProblemDetails is returned.
  • Status code is resolved from Result.Status using StatusCodeResolver (defaults: Failed= 400, Cancelled= 409, Error= 500, Completed not used)

Options

Options type comes from

public sealed class KnightResponseOptions
    : KnightResponseBaseOptions<ProblemDetails, ValidationProblemDetails>
{
    // Same core properties:
    // - IncludeFullResultPayload
    // - UseProblemDetails
    // - UseValidationProblemDetails
    // - IncludeExceptionDetails
    // - StatusCodeResolver
    // - ValidationMapper (default: DefaultValidationErrorMapper from Abstractions)
    // - ProblemDetailsBuilder, ValidationBuilder
}

Register and customize:

builder.Services.AddKnightResponse(options =>
{
    options.IncludeFullResultPayload    = true;
    options.UseProblemDetails           = true;
    options.UseValidationProblemDetails = true;

    options.StatusCodeResolver = status => status switch
    {
        Status.Failed    => StatusCodes.Status400BadRequest,
        Status.Cancelled => StatusCodes.Status409Conflict,
        Status.Error     => StatusCodes.Status500InternalServerError,
        _                => StatusCodes.Status400BadRequest
    };
});

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+.

Note: Knight.Response.Mvc may reference Microsoft.AspNetCore.Mvc types (like ProblemDetails) purely for compatibility. This does not require ASP.NET Core runtime.


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 92 9/27/2025
2.0.0-preview03 175 9/25/2025
2.0.0-preview02 172 9/24/2025
2.0.0-preview01 231 9/24/2025
1.0.0 145 9/14/2025
0.1.0 153 9/14/2025

Changed:
           - Promoted from preview (0.1.0) to stable (1.0.0).
           - No breaking changes; API surface is unchanged.
           - Documentation updated to clarify supported frameworks and related packages.

           Features:
           - Convert `Result` / `Result<T>` to `IActionResult` via `ResultExtensions`.
           - `ApiResults` factory for consistent HTTP responses (200, 201, 202, 204, 400, 404, 409).
           - `ProblemFactory` for building RFC 7807-style error objects.
           - `CompatProblemDetails` and `CompatValidationProblemDetails` for MVC compatibility.
           - Configurable via `KnightResponseOptions`.
           - Fully unit tested and mutation tested for reliability.