diff --git a/src/Microsoft.AspNet.Mvc.Extensions/JsonResult.cs b/src/Microsoft.AspNet.Mvc.Extensions/JsonResult.cs
index e4cfdfe4c1..a3874dc2c4 100644
--- a/src/Microsoft.AspNet.Mvc.Extensions/JsonResult.cs
+++ b/src/Microsoft.AspNet.Mvc.Extensions/JsonResult.cs
@@ -3,8 +3,9 @@
using System.Text;
using System.Threading.Tasks;
-using Microsoft.AspNet.Mvc.Internal;
+using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
+using Microsoft.Framework.OptionsModel;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
@@ -27,8 +28,8 @@ namespace Microsoft.AspNet.Mvc
///
/// The value to format as JSON.
public JsonResult(object value)
- : this(value, serializerSettings: SerializerSettingsProvider.CreateSerializerSettings())
{
+ Value = value;
}
///
@@ -86,12 +87,23 @@ namespace Microsoft.AspNet.Mvc
response.StatusCode = StatusCode.Value;
}
+ var serializerSettings = _serializerSettings;
+ if (serializerSettings == null)
+ {
+ serializerSettings = context
+ .HttpContext
+ .RequestServices
+ .GetRequiredService>()
+ .Options
+ .SerializerSettings;
+ }
+
using (var writer = new HttpResponseStreamWriter(response.Body, contentTypeHeader.Encoding))
{
using (var jsonWriter = new JsonTextWriter(writer))
{
jsonWriter.CloseOutput = false;
- var jsonSerializer = JsonSerializer.Create(_serializerSettings);
+ var jsonSerializer = JsonSerializer.Create(serializerSettings);
jsonSerializer.Serialize(jsonWriter, Value);
}
}
diff --git a/src/Microsoft.AspNet.Mvc.Extensions/ViewComponents/JsonViewComponentResult.cs b/src/Microsoft.AspNet.Mvc.Extensions/ViewComponents/JsonViewComponentResult.cs
index 3a18f9dd27..f88ce44b3c 100644
--- a/src/Microsoft.AspNet.Mvc.Extensions/ViewComponents/JsonViewComponentResult.cs
+++ b/src/Microsoft.AspNet.Mvc.Extensions/ViewComponents/JsonViewComponentResult.cs
@@ -2,8 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
-using Microsoft.AspNet.Mvc.Internal;
+using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
+using Microsoft.Framework.OptionsModel;
using Newtonsoft.Json;
namespace Microsoft.AspNet.Mvc
@@ -20,8 +21,8 @@ namespace Microsoft.AspNet.Mvc
///
/// The value to format as JSON text.
public JsonViewComponentResult(object value)
- : this(value, serializerSettings: SerializerSettingsProvider.CreateSerializerSettings())
{
+ Value = value;
}
///
@@ -47,10 +48,22 @@ namespace Microsoft.AspNet.Mvc
/// The .
public void Execute([NotNull] ViewComponentContext context)
{
+ var serializerSettings = _serializerSettings;
+ if (serializerSettings == null)
+ {
+ serializerSettings = context
+ .ViewContext
+ .HttpContext
+ .RequestServices
+ .GetRequiredService>()
+ .Options
+ .SerializerSettings;
+ }
+
using (var jsonWriter = new JsonTextWriter(context.Writer))
{
jsonWriter.CloseOutput = false;
- var jsonSerializer = JsonSerializer.Create(_serializerSettings);
+ var jsonSerializer = JsonSerializer.Create(serializerSettings);
jsonSerializer.Serialize(jsonWriter, Value);
}
}
diff --git a/test/Microsoft.AspNet.Mvc.Extensions.Test/JsonResultTest.cs b/test/Microsoft.AspNet.Mvc.Extensions.Test/JsonResultTest.cs
index bd59594666..cd5d36b50c 100644
--- a/test/Microsoft.AspNet.Mvc.Extensions.Test/JsonResultTest.cs
+++ b/test/Microsoft.AspNet.Mvc.Extensions.Test/JsonResultTest.cs
@@ -1,12 +1,14 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Routing;
+using Microsoft.Framework.DependencyInjection;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using Xunit;
@@ -109,15 +111,18 @@ namespace Microsoft.AspNet.Mvc
Assert.Equal("application/json; charset=utf-8", context.Response.ContentType);
}
- private HttpContext GetHttpContext()
+ private static HttpContext GetHttpContext()
{
var httpContext = new DefaultHttpContext();
httpContext.Response.Body = new MemoryStream();
+ var services = new ServiceCollection();
+ services.AddOptions();
+ httpContext.RequestServices = services.BuildServiceProvider();
return httpContext;
}
- private byte[] GetWrittenBytes(HttpContext context)
+ private static byte[] GetWrittenBytes(HttpContext context)
{
context.Response.Body.Seek(0, SeekOrigin.Begin);
return Assert.IsType(context.Response.Body).ToArray();
diff --git a/test/Microsoft.AspNet.Mvc.Extensions.Test/ViewComponents/JsonViewComponentResultTest.cs b/test/Microsoft.AspNet.Mvc.Extensions.Test/ViewComponents/JsonViewComponentResultTest.cs
index 7bad79686b..76e469b1f4 100644
--- a/test/Microsoft.AspNet.Mvc.Extensions.Test/ViewComponents/JsonViewComponentResultTest.cs
+++ b/test/Microsoft.AspNet.Mvc.Extensions.Test/ViewComponents/JsonViewComponentResultTest.cs
@@ -3,11 +3,13 @@
using System.IO;
using System.Text;
+using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Mvc.ViewComponents;
using Microsoft.AspNet.Routing;
+using Microsoft.Framework.DependencyInjection;
using Moq;
using Newtonsoft.Json;
using Xunit;
@@ -17,7 +19,7 @@ namespace Microsoft.AspNet.Mvc
public class JsonViewComponentResultTest
{
[Fact]
- public void Execute_UsesFormatter_WithSpecifiedSerializerSettings()
+ public void Execute_UsesSerializer_WithSpecifiedSerializerSettings()
{
// Arrange
var view = Mock.Of();
@@ -37,9 +39,27 @@ namespace Microsoft.AspNet.Mvc
Assert.Equal("{\r\n \"foo\": \"abcd\"\r\n}", Encoding.UTF8.GetString(buffer.ToArray()));
}
+ [Fact]
+ public void Execute_UsesSerializerSettingsFromOptions_IfNotProvided()
+ {
+ // Arrange
+ var view = Mock.Of();
+ var buffer = new MemoryStream();
+ var viewComponentContext = GetViewComponentContext(view, buffer);
+
+ var result = new JsonViewComponentResult(new { foo = "abcd" });
+ viewComponentContext.ViewContext.HttpContext.Response.Body = buffer;
+
+ // Act
+ result.Execute(viewComponentContext);
+
+ // Assert
+ Assert.Equal("{\"foo\":\"abcd\"}", Encoding.UTF8.GetString(buffer.ToArray()));
+ }
+
private static ViewComponentContext GetViewComponentContext(IView view, Stream stream)
{
- var actionContext = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
+ var actionContext = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor());
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
var viewContext = new ViewContext(
actionContext,
@@ -59,5 +79,15 @@ namespace Microsoft.AspNet.Mvc
var viewComponentContext = new ViewComponentContext(viewComponentDescriptor, new object[0], viewContext, writer);
return viewComponentContext;
}
+
+ private static HttpContext GetHttpContext()
+ {
+ var httpContext = new DefaultHttpContext();
+ var services = new ServiceCollection();
+ services.AddOptions();
+ httpContext.RequestServices = services.BuildServiceProvider();
+
+ return httpContext;
+ }
}
}
\ No newline at end of file