Codebelt.Extensions.Carter.AspNetCore.Text.Yaml Namespace
Enable YAML response negotiation in your Carter application with the Codebelt.Extensions.Carter.AspNetCore.Text.Yaml namespace. The YamlResponseNegotiator type, powered by YamlDotNet, lets your endpoints produce YAML responses through the standard HttpResponse.Negotiate() pipeline.
This negotiator uses YamlFormatterOptions to configure serialization settings, supported media types, and character encoding. It is well-suited for tooling, configuration APIs, DevOps scenarios, and any environment where YAML is the preferred interchange format.
Availability: .NET 10
When to use
Choose YamlResponseNegotiator when your API consumers expect YAML-formatted responses—common in CI/CD tooling, configuration management, infrastructure-as-code services, and cross-platform build pipelines where YAML's human readability and comment support add value over JSON.
Start here
Install the Codebelt.Extensions.Carter.AspNetCore.Text.Yaml NuGet package, then register YamlResponseNegotiator as the response negotiator in your Carter configuration via CarterConfigurator.WithResponseNegotiator<T>().
Classes
- YamlResponseNegotiator
-
The
YamlResponseNegotiatorintegrates YamlDotNet serialization into the Carter response pipeline. Register it as the response negotiator to have allHttpResponse.Negotiate()calls produce YAML output—ideal for tooling, configuration, and DevOps-focused APIs.Availability: .NET 10
Example
YamlResponseNegotiatoris constructed with anIOptions<YamlFormatterOptions>that controls supported media types and YamlDotNet serialization settings. Before serializing a model, the negotiator tests the client'sAcceptheader viaCanHandle; only when it returnstrueisHandlecalled to produce YAML output.Handleresolves the character encoding from theAccept-Charsetheader (falling back toYamlFormatterOptions.Encoding), sets theContent-Typeresponse header, and streams the serialized YAML directly to the response body.The following example constructs the negotiator with default options, confirms that the
application/yamlmedia type is accepted, then writes an anonymous object to an in-memory response stream and reads back the serialized output:using System; using System.IO; using System.Text; using System.Threading; using System.Threading.Tasks; using Codebelt.Extensions.Carter.AspNetCore.Text.Yaml; using Codebelt.Extensions.YamlDotNet.Formatters; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using Microsoft.Net.Http.Headers; namespace DocfxExample; public static class YamlResponseNegotiatorExample { public static async Task DemonstrateAsync() { var options = Options.Create(new YamlFormatterOptions()); var negotiator = new YamlResponseNegotiator(options); var acceptHeader = MediaTypeHeaderValue.Parse("application/yaml"); if (negotiator.CanHandle(acceptHeader)) { var context = new DefaultHttpContext(); context.Request.Headers.Accept = "application/yaml"; var model = new { Name = "YamlDotNet", Value = 42 }; await negotiator.Handle(context.Request, context.Response, model, CancellationToken.None); context.Response.Body.Position = 0; using var reader = new StreamReader(context.Response.Body, Encoding.UTF8); Console.WriteLine(await reader.ReadToEndAsync()); } } }