Codebelt

Cuemon.Extensions.AspNetCore.Mvc.Formatters.Text.Json

Registers Cuemon's System.Text.Json formatters in the ASP.NET Core MVC pipeline with one configuration model.

.NET 10.0 / .NET 9.0 MIT v10.5.3 18,479 downloads

Overview

Cuemon.Extensions.AspNetCore.Mvc.Formatters.Text.Json plugs Cuemon's JsonFormatter into the ASP.NET Core MVC formatter pipeline. It extends Cuemon.Extensions.AspNetCore.Text.Json with IMvcBuilder and IMvcCoreBuilder registration methods plus concrete MVC input and output formatters, so controller endpoints can read and write JSON through the same JsonFormatterOptions model.

The package keeps the MVC integration small and explicit. JsonSerializationMvcOptionsSetup inserts the Cuemon formatters at the front of MvcOptions, while the formatter constructors add the configured media types and the HTTP exception descriptor converter that respects SensitivityDetails.

Key APIs

MvcBuilderExtensions.AddJsonFormatters registers JsonSerializationMvcOptionsSetup for the MVC builder and then applies the package's JSON formatter options path, making it the main entry point for AddControllers() scenarios.

MvcBuilderExtensions.AddJsonFormattersOptions configures JsonFormatterOptions through the lower-level ASP.NET Core JSON package without inserting formatter instances on its own.

MvcCoreBuilderExtensions.AddJsonFormatters gives the same formatter registration behavior to AddMvcCore() pipelines, which is the path used in the package's controller integration tests.

MvcCoreBuilderExtensions.AddJsonFormattersOptions exposes the options-only path for IMvcCoreBuilder, so a core MVC setup can share the same JsonFormatterOptions configuration model.

JsonSerializationInputFormatter deserializes request bodies with JsonFormatter; the tests verify UTF-8 and UTF-16 encodings, and the constructor copies the configured media types into MVC's supported media type collection.

JsonSerializationOutputFormatter mirrors that setup for responses, using the same formatter options and supported media types when MVC serializes controller results.

JsonSerializationMvcOptionsSetup is the ConfigureOptions<MvcOptions> implementation that inserts the output formatter first and the input formatter second, so Cuemon's JSON formatters are considered before later MVC registrations.

Basic usage

using System.Linq;
using Codebelt.Extensions.Xunit;
using Cuemon.Extensions.AspNetCore.Mvc.Formatters.Text.Json;
using Cuemon.Extensions.Text.Json.Formatters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Xunit;

namespace MyProject.Tests;

public class JsonFormatterRegistrationTest : Test
{
    public JsonFormatterRegistrationTest(ITestOutputHelper output) : base(output)
    {
    }

    [Fact]
    public void AddJsonFormatters_ShouldRegisterCuemonMvcFormatters()
    {
        var services = new ServiceCollection();
        services.AddControllers().AddJsonFormatters(options => options.Settings.WriteIndented = true);

        using var provider = services.BuildServiceProvider();
        var formatterOptions = provider.GetRequiredService<IOptions<JsonFormatterOptions>>().Value;
        var mvcOptions = provider.GetRequiredService<IOptions<MvcOptions>>().Value;
        var inputMediaTypes = Assert.IsType<JsonSerializationInputFormatter>(mvcOptions.InputFormatters[0]).SupportedMediaTypes.Select(mediaType => mediaType.ToString()).ToArray();
        var outputMediaTypes = Assert.IsType<JsonSerializationOutputFormatter>(mvcOptions.OutputFormatters[0]).SupportedMediaTypes.Select(mediaType => mediaType.ToString()).ToArray();

        Assert.True(formatterOptions.Settings.WriteIndented);
        Assert.Contains("application/json", inputMediaTypes);
        Assert.Contains("text/json", outputMediaTypes);
        TestOutput.WriteLine(string.Join(", ", inputMediaTypes));
    }
}

Use this pattern when controller registration should move Cuemon JSON formatting and formatter options into MVC in one call. It matters because the package inserts its request and response formatters first, so the same JsonFormatterOptions configuration drives MVC serialization behavior.

Installation

dotnet add package Cuemon.Extensions.AspNetCore.Mvc.Formatters.Text.Json

Usage guidance

Adopt this package when your ASP.NET Core MVC application should negotiate JSON through Cuemon's formatter stack and you want formatter registration plus JsonFormatterOptions configuration to stay coupled. If you only need lower-level JsonFormatter configuration outside MVC, or your application is satisfied with the framework's default JSON formatters, use Cuemon.Extensions.AspNetCore.Text.Json, Cuemon.Extensions.Text.Json, or plain framework registration instead.

Family packages