Codebelt

Codebelt.Extensions.AspNetCore.Mvc.Formatters.Text.Yaml

Add YAML request and response formatters to ASP.NET Core MVC through IMvcBuilder and IMvcCoreBuilder extensions.

.NET 10.0 / .NET 9.0 MIT v10.1.4 5,598 downloads

Overview

Codebelt.Extensions.AspNetCore.Mvc.Formatters.Text.Yaml adds YAML request and response formatters to the ASP.NET Core MVC formatter pipeline. It extends both IMvcBuilder and IMvcCoreBuilder, so controller-based applications can negotiate YAML through the same MvcOptions infrastructure used for other media types.

The package builds on Codebelt.Extensions.AspNetCore.Text.Yaml for YamlFormatterOptions configuration and YAML exception response formatting. Its own responsibility is the MVC-specific registration and formatter types that connect YamlFormatter to MvcOptions.

Key APIs

MvcBuilderExtensions.AddYamlFormatters registers YamlSerializationMvcOptionsSetup for an IMvcBuilder, then applies YAML formatter options and YAML exception response formatter services through the lower-level ASP.NET Core YAML package.

MvcCoreBuilderExtensions.AddYamlFormatters does the same work for IMvcCoreBuilder, which fits slimmer MVC Core setups that still need YAML input and output formatters.

MvcBuilderExtensions.AddYamlFormattersOptions configures YamlFormatterOptions and the YAML exception response formatter for an IMvcBuilder without adding the MVC formatter setup a second time.

MvcCoreBuilderExtensions.AddYamlFormattersOptions provides the same configuration-only path for IMvcCoreBuilder, which is useful when formatter registration and formatter configuration need to be separated.

YamlSerializationMvcOptionsSetup is the ConfigureOptions<MvcOptions> implementation that inserts YamlSerializationOutputFormatter and YamlSerializationInputFormatter at index 0, making YAML available through the MVC formatter collections.

YamlSerializationInputFormatter derives from StreamInputFormatter<YamlFormatter, YamlFormatterOptions>, adds the configured supported media types, and registers the HttpExceptionDescriptor YAML converter with the formatter's sensitivity settings.

YamlSerializationOutputFormatter derives from StreamOutputFormatter<YamlFormatter, YamlFormatterOptions> and mirrors the same media-type and exception-converter setup for response serialization.

Basic usage

using Codebelt.Extensions.AspNetCore.Mvc.Formatters.Text.Yaml;
using Codebelt.Extensions.Xunit;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Xunit;

namespace MyProject.Tests;

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

    [Fact]
    public void ShouldRegisterYamlMvcFormatters()
    {
        var services = new ServiceCollection();
        services.AddMvcCore().AddYamlFormatters();

        using var provider = services.BuildServiceProvider();
        var mvcOptions = provider.GetRequiredService<IOptions<MvcOptions>>().Value;

        var inputFormatter = Assert.IsType<YamlSerializationInputFormatter>(mvcOptions.InputFormatters[0]);
        var outputFormatter = Assert.IsType<YamlSerializationOutputFormatter>(mvcOptions.OutputFormatters[0]);

        TestOutput.WriteLine($"Input: {inputFormatter.GetType().Name}, Output: {outputFormatter.GetType().Name}");
    }
}

Use this pattern when a controller-based ASP.NET Core application should negotiate YAML through MVC without manually constructing formatter instances. It matters because the package wires the MVC formatter registrations and shared YAML options into the same service collection entry point.

Installation

dotnet add package Codebelt.Extensions.AspNetCore.Mvc.Formatters.Text.Yaml

Usage guidance

Adopt this package when an MVC or MVC Core application needs YAML request and response bodies to participate in the normal ASP.NET Core formatter pipeline, and when the same YAML configuration should also drive YAML-formatted exception responses. If you only need YAML serialization services or exception response formatting outside MVC, use Codebelt.Extensions.AspNetCore.Text.Yaml instead, and if your application does not need YAML media types, the built-in framework formatters are the simpler choice.

Family packages