Added overload to ViewComponent.Json to accept JsonSerializerSettings
This commit is contained in:
parent
7767251dad
commit
39642761b1
|
|
@ -4,6 +4,7 @@
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Microsoft.Framework.Internal;
|
using Microsoft.Framework.Internal;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc
|
namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
|
|
@ -17,8 +18,19 @@ namespace Microsoft.AspNet.Mvc
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The value to format as JSON text.</param>
|
/// <param name="value">The value to format as JSON text.</param>
|
||||||
public JsonViewComponentResult(object value)
|
public JsonViewComponentResult(object value)
|
||||||
|
: this(value, formatter: null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new <see cref="JsonViewComponentResult"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value to format as JSON text.</param>
|
||||||
|
/// <param name="serializerSettings">The <see cref="JsonSerializerSettings"/> to be used by
|
||||||
|
/// the formatter.</param>
|
||||||
|
public JsonViewComponentResult(object value, [NotNull] JsonSerializerSettings serializerSettings)
|
||||||
|
: this(value, new JsonOutputFormatter(serializerSettings))
|
||||||
{
|
{
|
||||||
Value = value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ using Microsoft.AspNet.Mvc.ModelBinding;
|
||||||
using Microsoft.AspNet.Mvc.Rendering;
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
using Microsoft.AspNet.Routing;
|
using Microsoft.AspNet.Routing;
|
||||||
using Microsoft.Framework.Internal;
|
using Microsoft.Framework.Internal;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc
|
namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
|
|
@ -167,11 +168,25 @@ namespace Microsoft.AspNet.Mvc
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The value to output in JSON text.</param>
|
/// <param name="value">The value to output in JSON text.</param>
|
||||||
/// <returns>A <see cref="JsonViewComponentResult"/>.</returns>
|
/// <returns>A <see cref="JsonViewComponentResult"/>.</returns>
|
||||||
public JsonViewComponentResult Json([NotNull] object value)
|
public JsonViewComponentResult Json(object value)
|
||||||
{
|
{
|
||||||
return new JsonViewComponentResult(value);
|
return new JsonViewComponentResult(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a result which will render JSON text.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value to output in JSON text.</param>
|
||||||
|
/// <param name="serializerSettings">The <see cref="JsonSerializerSettings"/> to be used by
|
||||||
|
/// the formatter.</param>
|
||||||
|
/// <returns>A <see cref="JsonViewComponentResult"/>.</returns>
|
||||||
|
/// <remarks>Callers should cache an instance of <see cref="JsonSerializerSettings"/> to avoid
|
||||||
|
/// recreating cached data with each call.</remarks>
|
||||||
|
public JsonViewComponentResult Json(object value, [NotNull] JsonSerializerSettings serializerSettings)
|
||||||
|
{
|
||||||
|
return new JsonViewComponentResult(value, serializerSettings);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a result which will render the partial view with name <c>"Default"</c>.
|
/// Returns a result which will render the partial view with name <c>"Default"</c>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -1074,6 +1074,33 @@ namespace Microsoft.AspNet.Mvc.Test
|
||||||
Times.Once());
|
Times.Once());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Controller_JsonWithParameterValueAndSerializerSettings_IDisposableObject_RegistersForDispose()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var mockHttpContext = new Mock<DefaultHttpContext>();
|
||||||
|
mockHttpContext.Setup(x => x.Response.OnResponseCompleted(It.IsAny<Action<object>>(), It.IsAny<object>()));
|
||||||
|
|
||||||
|
var controller = new TestableController()
|
||||||
|
{
|
||||||
|
ActionContext = new ActionContext(mockHttpContext.Object, new RouteData(), new ActionDescriptor())
|
||||||
|
};
|
||||||
|
var input = new DisposableObject();
|
||||||
|
var serializerSettings = new JsonSerializerSettings();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = controller.Json(input, serializerSettings);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsType<JsonResult>(result);
|
||||||
|
Assert.Same(input, result.Value);
|
||||||
|
var jsonFormatter = result.Formatter as JsonOutputFormatter;
|
||||||
|
Assert.Same(serializerSettings, jsonFormatter.SerializerSettings);
|
||||||
|
mockHttpContext.Verify(
|
||||||
|
x => x.Response.OnResponseCompleted(It.IsAny<Action<object>>(), It.IsAny<object>()),
|
||||||
|
Times.Once());
|
||||||
|
}
|
||||||
|
|
||||||
public static IEnumerable<object[]> RedirectTestData
|
public static IEnumerable<object[]> RedirectTestData
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ using Microsoft.AspNet.Mvc.ViewComponents;
|
||||||
using Microsoft.AspNet.Routing;
|
using Microsoft.AspNet.Routing;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Moq;
|
using Moq;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc
|
namespace Microsoft.AspNet.Mvc
|
||||||
|
|
@ -36,6 +37,26 @@ namespace Microsoft.AspNet.Mvc
|
||||||
Assert.Equal("1", new StreamReader(buffer).ReadToEnd());
|
Assert.Equal("1", new StreamReader(buffer).ReadToEnd());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Execute_UsesFormatter_WithSpecifiedSerializerSettings()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var view = Mock.Of<IView>();
|
||||||
|
var buffer = new MemoryStream();
|
||||||
|
var viewComponentContext = GetViewComponentContext(view, buffer);
|
||||||
|
|
||||||
|
var serializerSettings = new JsonSerializerSettings();
|
||||||
|
serializerSettings.Formatting = Formatting.Indented;
|
||||||
|
|
||||||
|
var result = new JsonViewComponentResult("abc", serializerSettings);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
result.Execute(viewComponentContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Same(serializerSettings, result.Formatter.SerializerSettings);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Execute_FallsbackToServices_WhenNoJsonFormatterIsProvided()
|
public void Execute_FallsbackToServices_WhenNoJsonFormatterIsProvided()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue