diff --git a/src/Microsoft.AspNetCore.Mvc.Formatters.Json/DependencyInjection/MvcJsonMvcCoreBuilderExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Formatters.Json/DependencyInjection/MvcJsonMvcCoreBuilderExtensions.cs index 50e3a66aba..be0efb28b9 100644 --- a/src/Microsoft.AspNetCore.Mvc.Formatters.Json/DependencyInjection/MvcJsonMvcCoreBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Formatters.Json/DependencyInjection/MvcJsonMvcCoreBuilderExtensions.cs @@ -47,6 +47,30 @@ namespace Microsoft.Extensions.DependencyInjection return builder; } + /// + /// Adds configuration of for the application. + /// + /// The . + /// The which need to be configured. + /// The . + public static IMvcCoreBuilder AddJsonOptions( + this IMvcCoreBuilder builder, + Action setupAction) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (setupAction == null) + { + throw new ArgumentNullException(nameof(setupAction)); + } + + builder.Services.Configure(setupAction); + return builder; + } + // Internal for testing. internal static void AddJsonFormatterServices(IServiceCollection services) { diff --git a/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs index 12bac9568a..bf8cf2e720 100644 --- a/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs @@ -30,6 +30,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using Moq; +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json; using Xunit; namespace Microsoft.AspNetCore.Mvc @@ -226,6 +228,23 @@ namespace Microsoft.AspNetCore.Mvc Assert.Same(manager, descriptor.ImplementationInstance); } + [Fact] + public void AddMvcCore_AddsMvcJsonOption() + { + // Arrange + var services = new ServiceCollection(); + + // Act + services.AddMvcCore() + .AddJsonOptions((options) => + { + options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); + }); + + // Assert + Assert.Single(services, d => d.ServiceType == typeof(IConfigureOptions)); + } + private IEnumerable SingleRegistrationServiceTypes { get