From d7bfbb5824b5f8876bcd4afaa29a611efc7aa1c9 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 25 Jul 2019 16:50:54 -0700 Subject: [PATCH] Add formatter mapping for JSON (#12592) Fixes https://github.com/aspnet/AspNetCore/issues/12414 --- .../src/Infrastructure/MvcCoreMvcOptionsSetup.cs | 3 +++ src/Mvc/Mvc/test/MvcOptionsSetupTest.cs | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Mvc/Mvc.Core/src/Infrastructure/MvcCoreMvcOptionsSetup.cs b/src/Mvc/Mvc.Core/src/Infrastructure/MvcCoreMvcOptionsSetup.cs index 79005a8389..cbc141fe76 100644 --- a/src/Mvc/Mvc.Core/src/Infrastructure/MvcCoreMvcOptionsSetup.cs +++ b/src/Mvc/Mvc.Core/src/Infrastructure/MvcCoreMvcOptionsSetup.cs @@ -80,6 +80,9 @@ namespace Microsoft.AspNetCore.Mvc // Set up default input formatters. options.InputFormatters.Add(new SystemTextJsonInputFormatter(_jsonOptions.Value, _loggerFactory.CreateLogger())); + // Media type formatter mappings for JSON + options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValues.ApplicationJson); + // Set up default output formatters. options.OutputFormatters.Add(new HttpNoContentOutputFormatter()); options.OutputFormatters.Add(new StringOutputFormatter()); diff --git a/src/Mvc/Mvc/test/MvcOptionsSetupTest.cs b/src/Mvc/Mvc/test/MvcOptionsSetupTest.cs index ccd04a0cd4..00eda811ea 100644 --- a/src/Mvc/Mvc/test/MvcOptionsSetupTest.cs +++ b/src/Mvc/Mvc/test/MvcOptionsSetupTest.cs @@ -111,6 +111,17 @@ namespace Microsoft.AspNetCore.Mvc formatter => Assert.IsType(formatter)); } + [Fact] + public void Setup_SetsUpFormatterMapping() + { + // Arrange & Act + var options = GetOptions(); + + // Assert + var mapping = options.FormatterMappings.GetMediaTypeMappingForFormat("json"); + Assert.Equal("application/json", mapping); + } + [Fact] public void Setup_SetsUpModelValidatorProviders() {