Add IMvcCoreBuilder.AddJsonOptions extension method

Fixes #4967
This commit is contained in:
ivano scifoni 2016-07-06 19:18:09 +02:00 committed by Pranav K
parent 7430efa8cc
commit 3d2bb673ee
2 changed files with 43 additions and 0 deletions

View File

@ -47,6 +47,30 @@ namespace Microsoft.Extensions.DependencyInjection
return builder;
}
/// <summary>
/// Adds configuration of <see cref="MvcJsonOptions"/> for the application.
/// </summary>
/// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
/// <param name="setupAction">The <see cref="MvcJsonOptions"/> which need to be configured.</param>
/// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
public static IMvcCoreBuilder AddJsonOptions(
this IMvcCoreBuilder builder,
Action<MvcJsonOptions> setupAction)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (setupAction == null)
{
throw new ArgumentNullException(nameof(setupAction));
}
builder.Services.Configure<MvcJsonOptions>(setupAction);
return builder;
}
// Internal for testing.
internal static void AddJsonFormatterServices(IServiceCollection services)
{

View File

@ -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<MvcJsonOptions>));
}
private IEnumerable<Type> SingleRegistrationServiceTypes
{
get