Class MvcCoreBuilderExtensions
- Namespace
- Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json
- Assembly
- Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json.dll
Extension methods for the IMvcCoreBuilder interface.
public static class MvcCoreBuilderExtensions
- Inheritance
-
MvcCoreBuilderExtensions
Examples
AddMvcCore() provides minimal MVC services for advanced scenarios where you hand-select components, but doesn't automatically include input/output formatters—you must register them explicitly. Switching from System.Text.Json to Newtonsoft.Json with environment-aware exception sensitivity requires manually registering formatters and configuration. The MvcCoreBuilderExtensions class provides chainable extension methods on IMvcCoreBuilder that register Newtonsoft.Json formatters with consistent configuration, eliminating boilerplate and reducing integration errors. This example demonstrates registering and configuring Newtonsoft.Json formatters for advanced scenarios:
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 mvcCoreBuilder = builder.Services
.AddMvcCore()
.AddNewtonsoftJsonFormatters(options =>
{
options.SensitivityDetails = FaultSensitivityDetails.All;
});
mvcCoreBuilder.AddNewtonsoftJsonFormattersOptions(options =>
{
options.SensitivityDetails = FaultSensitivityDetails.All;
});
var app = builder.Build();
app.MapControllers();
app.Run();
}
}
Methods
AddNewtonsoftJsonFormatters(IMvcCoreBuilder, Action<NewtonsoftJsonFormatterOptions>)
Adds the JSON serializer formatters to MVC.
public static IMvcCoreBuilder AddNewtonsoftJsonFormatters(this IMvcCoreBuilder builder, Action<NewtonsoftJsonFormatterOptions> setup = null)
Parameters
builderIMvcCoreBuilderThe IMvcCoreBuilder.
setupAction<NewtonsoftJsonFormatterOptions>The NewtonsoftJsonFormatterOptions which may be configured.
Returns
- IMvcCoreBuilder
A reference to
builderafter the operation has completed.
Exceptions
- ArgumentNullException
buildercannot be null -or-setupcannot be null.
AddNewtonsoftJsonFormattersOptions(IMvcCoreBuilder, Action<NewtonsoftJsonFormatterOptions>)
Adds configuration of NewtonsoftJsonFormatterOptions for the application.
public static IMvcCoreBuilder AddNewtonsoftJsonFormattersOptions(this IMvcCoreBuilder builder, Action<NewtonsoftJsonFormatterOptions> setup = null)
Parameters
builderIMvcCoreBuilderThe IMvcBuilder.
setupAction<NewtonsoftJsonFormatterOptions>The NewtonsoftJsonFormatterOptions which need to be configured.
Returns
- IMvcCoreBuilder
A reference to
builderafter the operation has completed.
Exceptions
- ArgumentNullException
buildercannot be null.- ArgumentException
setupfailed to configure an instance of NewtonsoftJsonFormatterOptions in a valid state.