Table of Contents

ServiceCollectionExtensions Class

Definition

Namespace
Codebelt.Extensions.AspNetCore.Newtonsoft.Json
Assemblies
Codebelt.Extensions.AspNetCore.Newtonsoft.Json.dll
Source
src/Codebelt.Extensions.AspNetCore.Newtonsoft.Json/ServiceCollectionExtensions.cs

Extension methods for the IServiceCollection interface.

public static class ServiceCollectionExtensions
Inheritance
ServiceCollectionExtensions

Examples

ASP.NET Core applications need centralized exception response formatting that respects custom JSON serialization configuration. The AddMinimalNewtonsoftJsonOptions method registers an exception response formatter that applies Newtonsoft.Json serialization with configured sensitivity settings, ensuring consistent error responses across both controller-based and minimal API endpoints. This example demonstrates registering exception response formatter options:

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

namespace Examples;

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

        builder.Services.AddMinimalNewtonsoftJsonOptions(options =>
        {
            options.SensitivityDetails = FaultSensitivityDetails.All;
        });

        var app = builder.Build();
        Console.WriteLine("Newtonsoft.Json exception response formatter registered");
        app.Run();
    }
}

Methods

Name Description
AddMinimalNewtonsoftJsonOptions(IServiceCollection, Action<NewtonsoftJsonFormatterOptions>)

Adds a NewtonsoftJsonFormatterOptions service to the specified IServiceCollection.