Table of Contents

Class MvcBuilderExtensions

Namespace
Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json
Assembly
Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json.dll

Extension methods for the IMvcBuilder interface.

public static class MvcBuilderExtensions
Inheritance
MvcBuilderExtensions

Examples

ASP.NET Core AddControllers() configures default MVC services with System.Text.Json by default, which doesn't support custom Newtonsoft.Json converters or environment-aware exception sensitivity. Switching to Newtonsoft.Json requires registering input/output formatters with consistent configuration. The MvcBuilderExtensions class provides chainable extension methods on IMvcBuilder that register Newtonsoft.Json formatters and apply NewtonsoftJsonFormatterOptions, ensuring all MVC endpoints use consistent JSON serialization behavior. This example demonstrates registering and configuring Newtonsoft.Json formatters:

using Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json;
using Codebelt.Extensions.Newtonsoft.Json.Formatters;
using Cuemon.Diagnostics;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Examples;

class Program
{
    static void Main()
    {
        var builder = WebApplication.CreateBuilder();

        var mvcBuilder = builder.Services
            .AddControllers()
            .AddNewtonsoftJsonFormatters(options =>
            {
                options.SensitivityDetails = FaultSensitivityDetails.All;
            });

        mvcBuilder.AddNewtonsoftJsonFormattersOptions(options =>
        {
            options.SensitivityDetails = FaultSensitivityDetails.All;
        });

        var app = builder.Build();
        app.MapControllers();
        app.Run();
    }
}

Methods

AddNewtonsoftJsonFormatters(IMvcBuilder, Action<NewtonsoftJsonFormatterOptions>)

Adds the JSON serializer formatters to MVC.

public static IMvcBuilder AddNewtonsoftJsonFormatters(this IMvcBuilder builder, Action<NewtonsoftJsonFormatterOptions> setup = null)

Parameters

builder IMvcBuilder

The IMvcBuilder.

setup Action<NewtonsoftJsonFormatterOptions>

The NewtonsoftJsonFormatterOptions which may be configured.

Returns

IMvcBuilder

A reference to builder after the operation has completed.

Exceptions

ArgumentNullException

builder cannot be null -or- setup cannot be null.

AddNewtonsoftJsonFormattersOptions(IMvcBuilder, Action<NewtonsoftJsonFormatterOptions>)

Adds configuration of NewtonsoftJsonFormatterOptions for the application.

public static IMvcBuilder AddNewtonsoftJsonFormattersOptions(this IMvcBuilder builder, Action<NewtonsoftJsonFormatterOptions> setup = null)

Parameters

builder IMvcBuilder

The IMvcBuilder.

setup Action<NewtonsoftJsonFormatterOptions>

The NewtonsoftJsonFormatterOptions which need to be configured.

Returns

IMvcBuilder

A reference to builder after the operation has completed.

Exceptions

ArgumentNullException

builder cannot be null.

ArgumentException

setup failed to configure an instance of NewtonsoftJsonFormatterOptions in a valid state.