/
MvcCoreBuilderExtensions.cs
54 lines (52 loc) · 2.71 KB
/
MvcCoreBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using Cuemon.Extensions.AspNetCore.Newtonsoft.Json.Formatters;
using Cuemon.Extensions.Newtonsoft.Json.Formatters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace Cuemon.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json
{
/// <summary>
/// Extension methods for the <see cref="IMvcCoreBuilder"/> interface.
/// </summary>
public static class MvcCoreBuilderExtensions
{
/// <summary>
/// Adds the JSON serializer formatters to MVC.
/// </summary>
/// <param name="builder">The <see cref="IMvcCoreBuilder"/>.</param>
/// <param name="setup">The <see cref="NewtonsoftJsonFormatterOptions"/> which may be configured.</param>
/// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="builder"/> cannot be null -or-
/// <paramref name="setup"/> cannot be null.
/// </exception>
public static IMvcCoreBuilder AddNewtonsoftJsonFormatters(this IMvcCoreBuilder builder, Action<NewtonsoftJsonFormatterOptions> setup = null)
{
Validator.ThrowIfNull(builder);
builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, JsonSerializationMvcOptionsSetup>());
AddNewtonsoftJsonFormattersOptions(builder, setup);
return builder;
}
/// <summary>
/// Adds configuration of <see cref="NewtonsoftJsonFormatterOptions"/> for the application.
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
/// <param name="setup">The <see cref="NewtonsoftJsonFormatterOptions"/> which need to be configured.</param>
/// <returns>A reference to <paramref name="builder"/> after the operation has completed.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="builder"/> cannot be null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="setup"/> failed to configure an instance of <see cref="NewtonsoftJsonFormatterOptions"/> in a valid state.
/// </exception>
public static IMvcCoreBuilder AddNewtonsoftJsonFormattersOptions(this IMvcCoreBuilder builder, Action<NewtonsoftJsonFormatterOptions> setup = null)
{
Validator.ThrowIfNull(builder);
builder.Services.AddNewtonsoftJsonFormatterOptions(setup);
builder.Services.AddNewtonsoftJsonExceptionResponseFormatter();
return builder;
}
}
}