public Startup(IConfiguration configuration) { Configuration = configuration; _optionsResolver = new OptionsResolver(configuration); _connectionStringResolver = new ConnectionStringResolver(configuration); _customMediaTypeService = new CustomMediaTypeService(); }
public static IMvcBuilder AddControllers(this IServiceCollection services, ICustomMediaTypeService customMediaTypeService = null, Func <JsonOptions> optionsFunc = null) { var options = optionsFunc == null ? new JsonOptions() : optionsFunc(); var builder = services.AddControllers(o => { o.ConfigureNotAcceptableMediaTypes(); o.AddGlobalActionFilters(); }); if (options.UseNewtonSoftJson) { return(builder.ConfigureNewtonSoftJson(options, customMediaTypeService)); } return(builder.ConfigureTextJson(options, customMediaTypeService)); }
public static MvcOptions AddCustomOutputFormatters <TJsonOutputFormatter>(this MvcOptions options, ICustomMediaTypeService customMediaTypeService) where TJsonOutputFormatter : OutputFormatter { var xmlOutputFormatter = new XmlDataContractSerializerOutputFormatter(); options.OutputFormatters.Add(xmlOutputFormatter); if (customMediaTypeService == null) { return(options); } customMediaTypeService.GetXmlTypes().Execute(e => xmlOutputFormatter.SupportedMediaTypes.Add(e)); var jsonOutputFormatter = options.OutputFormatters .OfType <TJsonOutputFormatter>() .First(); customMediaTypeService.GetJsonTypes().Execute(e => jsonOutputFormatter.SupportedMediaTypes.Add(e)); return(options); }
public static IMvcBuilder ConfigureTextJson(this IMvcBuilder builder, JsonOptions options, ICustomMediaTypeService customMediaTypeService) { if (options == null) { throw new ArgumentNullException(nameof(options)); } return(builder.AddJsonOptions(o => { o.JsonSerializerOptions.AllowTrailingCommas = options.BeTolerant; o.JsonSerializerOptions.PropertyNameCaseInsensitive = options.BeTolerant; if (options.BeTolerant) { o.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip; } o.JsonSerializerOptions.IgnoreNullValues = options.IgnoreNullValue; o.JsonSerializerOptions.PropertyNamingPolicy = options.UseCamelCase ? JsonNamingPolicy.CamelCase : null; if (options.EnumAsString) { o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); } }).AddMvcOptions(o => { o.AddCustomInputFormatters(); o.AddCustomOutputFormatters <SystemTextJsonOutputFormatter>(customMediaTypeService); })); }
public static IMvcBuilder ConfigureNewtonSoftJson(this IMvcBuilder builder, JsonOptions options, ICustomMediaTypeService customMediaTypeService) { if (options == null) { throw new ArgumentNullException(nameof(options)); } return(builder.AddNewtonsoftJson(o => { if (options.UseCamelCase) { o.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); } if (options.IgnoreNullValue) { o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; } if (options.EnumAsString) { o.SerializerSettings.Converters.Add(new StringEnumConverter()); } if (options.SerializeReferenceLoop) { o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize; } }).AddMvcOptions(o => { o.AddCustomInputFormatters(); o.AddCustomOutputFormatters <NewtonsoftJsonOutputFormatter>(customMediaTypeService); })); }
public static IServiceCollection AddCompression(this IServiceCollection services, Func <CompressionOptions> optionsFunc, ICustomMediaTypeService customMediaTypeService = null) { var options = optionsFunc(); if (!options.EnableCompression) { return(services); } services.AddResponseCompression(o => { o.EnableForHttps = options.EnableForHttps; if (customMediaTypeService != null) { o.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(customMediaTypeService.GetAll()); } else { o.MimeTypes = ResponseCompressionDefaults.MimeTypes; } }); return(services); }