* Remove JsonViewComponentResult
This commit is contained in:
parent
7b70da14bc
commit
f1fa1bd8f4
|
|
@ -13,7 +13,6 @@ using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
|||
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc
|
||||
{
|
||||
|
|
@ -215,35 +214,6 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
return new ContentViewComponentResult(content);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result which will render JSON text.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to output in JSON text.</param>
|
||||
/// <returns>A <see cref="JsonViewComponentResult"/>.</returns>
|
||||
public JsonViewComponentResult Json(object 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, JsonSerializerSettings serializerSettings)
|
||||
{
|
||||
if (serializerSettings == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(serializerSettings));
|
||||
}
|
||||
|
||||
return new JsonViewComponentResult(value, serializerSettings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result which will render the partial view with name <c>"Default"</c>.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -1,97 +0,0 @@
|
|||
// 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.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc.ViewComponents
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="IViewComponentResult"/> which renders JSON text when executed.
|
||||
/// </summary>
|
||||
public class JsonViewComponentResult : IViewComponentResult
|
||||
{
|
||||
private readonly JsonSerializerSettings _serializerSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new <see cref="JsonViewComponentResult"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to format as JSON text.</param>
|
||||
public JsonViewComponentResult(object value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <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, JsonSerializerSettings serializerSettings)
|
||||
{
|
||||
if (serializerSettings == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(serializerSettings));
|
||||
}
|
||||
|
||||
Value = value;
|
||||
_serializerSettings = serializerSettings;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value.
|
||||
/// </summary>
|
||||
public object Value { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Renders JSON text to the output.
|
||||
/// </summary>
|
||||
/// <param name="context">The <see cref="ViewComponentContext"/>.</param>
|
||||
public void Execute(ViewComponentContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
var serializerSettings = _serializerSettings;
|
||||
if (serializerSettings == null)
|
||||
{
|
||||
serializerSettings = context
|
||||
.ViewContext
|
||||
.HttpContext
|
||||
.RequestServices
|
||||
.GetRequiredService<IOptions<MvcJsonOptions>>()
|
||||
.Value
|
||||
.SerializerSettings;
|
||||
}
|
||||
|
||||
using (var jsonWriter = new JsonTextWriter(context.Writer))
|
||||
{
|
||||
jsonWriter.CloseOutput = false;
|
||||
var jsonSerializer = JsonSerializer.Create(serializerSettings);
|
||||
jsonSerializer.Serialize(jsonWriter, Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Renders JSON text to the output.
|
||||
/// </summary>
|
||||
/// <param name="context">The <see cref="ViewComponentContext"/>.</param>
|
||||
/// <returns>A completed <see cref="Task"/>.</returns>
|
||||
public Task ExecuteAsync(ViewComponentContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
Execute(context);
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -217,27 +217,6 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
|||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task JsonViewComponent_RendersJson()
|
||||
{
|
||||
// Arrange
|
||||
var expectedBody = JsonConvert.SerializeObject(new BasicWebSite.Models.Person()
|
||||
{
|
||||
Id = 10,
|
||||
Name = "John"
|
||||
});
|
||||
|
||||
// Act
|
||||
var response = await Client.GetAsync("Home/JsonTextInView");
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Equal("text/html", response.Content.Headers.ContentType.MediaType);
|
||||
|
||||
var actualBody = await response.Content.ReadAsStringAsync();
|
||||
Assert.Equal(expectedBody, actualBody);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task JsonHelper_RendersJson()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -33,11 +33,11 @@
|
|||
|
||||
<div>
|
||||
<h3 style="font-family: cursive;">Current Tag Cloud from Tag Helper</h3>
|
||||
<div>["Lorem","ipsum","dolor","sit","amet","consectetur","adipisicing","elit","sed","do","eiusmod","tempor","incididunt","ut","labore","et","dolore","magna","aliquaUt","enim"]</div>
|
||||
<div>Lorem,ipsum,dolor,sit,amet,consectetur,adipisicing,elit,sed,do,eiusmod,tempor,incididunt,ut,labore,et,dolore,magna,aliquaUt,enim</div>
|
||||
<h3 style="font-family: cursive;">Current Tag Cloud from ViewComponentHelper:</h3>
|
||||
<section><b>["Lorem","ipsum","dolor","sit","amet","consectetur","adipisicing","elit","sed","do","eiusmod","tempor","incididunt","ut","labore"]</b></section>
|
||||
<section><b>Lorem,ipsum,dolor,sit,amet,consectetur,adipisicing,elit,sed,do,eiusmod,tempor,incididunt,ut,labore</b></section>
|
||||
<br /><p><em>Rendering Template:</em></p>
|
||||
<div><h3 style="font-family: cursive;">Tag Cloud from Template: </h3>["Lorem","ipsum","dolor","sit","amet","consectetur","adipisicing","elit","sed","do","eiusmod","tempor","incididunt","ut","labore","et","dolore","magna","aliquaUt","enim"]</div></div>
|
||||
<div><h3 style="font-family: cursive;">Tag Cloud from Template: </h3>Lorem,ipsum,dolor,sit,amet,consectetur,adipisicing,elit,sed,do,eiusmod,tempor,incididunt,ut,labore,et,dolore,magna,aliquaUt,enim</div></div>
|
||||
|
||||
|
||||
<hr />
|
||||
|
|
|
|||
|
|
@ -59,21 +59,6 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
Assert.Same(expectedContent, actualResult.Content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ViewComponent_Json_SetsResultData()
|
||||
{
|
||||
// Arrange
|
||||
var viewComponent = new TestViewComponent();
|
||||
var testData = new object();
|
||||
|
||||
// Act
|
||||
var actualResult = viewComponent.Json(testData);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<JsonViewComponentResult>(actualResult);
|
||||
Assert.Same(testData, actualResult.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ViewComponent_View_WithEmptyParameter_SetsResultViewWithDefaultViewName()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,108 +0,0 @@
|
|||
// 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.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Internal;
|
||||
using Microsoft.AspNetCore.Mvc.Abstractions;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.AspNetCore.Mvc.ViewComponents;
|
||||
using Microsoft.AspNetCore.Mvc.ViewEngines;
|
||||
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.WebEncoders.Testing;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc
|
||||
{
|
||||
public class JsonViewComponentResultTest
|
||||
{
|
||||
[Fact]
|
||||
public void Execute_UsesSerializer_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(new { foo = "abcd" }, serializerSettings);
|
||||
viewComponentContext.ViewContext.HttpContext.Response.Body = buffer;
|
||||
|
||||
// Act
|
||||
result.Execute(viewComponentContext);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(
|
||||
$"{{{Environment.NewLine} \"foo\": \"abcd\"{Environment.NewLine}}}",
|
||||
Encoding.UTF8.GetString(buffer.ToArray()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Execute_UsesSerializerSettingsFromOptions_IfNotProvided()
|
||||
{
|
||||
// Arrange
|
||||
var view = Mock.Of<IView>();
|
||||
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(GetHttpContext(), new RouteData(), new ActionDescriptor());
|
||||
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
|
||||
var viewContext = new ViewContext(
|
||||
actionContext,
|
||||
view,
|
||||
viewData,
|
||||
Mock.Of<ITempDataDictionary>(),
|
||||
TextWriter.Null,
|
||||
new HtmlHelperOptions());
|
||||
|
||||
var writer = new StreamWriter(stream) { AutoFlush = true };
|
||||
|
||||
var viewComponentDescriptor = new ViewComponentDescriptor()
|
||||
{
|
||||
TypeInfo = typeof(object).GetTypeInfo(),
|
||||
};
|
||||
|
||||
var viewComponentContext = new ViewComponentContext(
|
||||
viewComponentDescriptor,
|
||||
new Dictionary<string, object>(),
|
||||
new HtmlTestEncoder(),
|
||||
viewContext,
|
||||
writer);
|
||||
|
||||
return viewComponentContext;
|
||||
}
|
||||
|
||||
private static HttpContext GetHttpContext()
|
||||
{
|
||||
var httpContext = new DefaultHttpContext();
|
||||
var services = new ServiceCollection();
|
||||
services.AddOptions();
|
||||
httpContext.RequestServices = services.BuildServiceProvider();
|
||||
|
||||
return httpContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
// 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 BasicWebSite.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace BasicWebSite.Components
|
||||
{
|
||||
[ViewComponent(Name = "JsonTextInView")]
|
||||
public class JsonTextInView : ViewComponent
|
||||
{
|
||||
public IViewComponentResult Invoke()
|
||||
{
|
||||
return Json(new Person()
|
||||
{
|
||||
Id = 10,
|
||||
Name = "John"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -83,11 +83,6 @@ namespace BasicWebSite.Controllers
|
|||
return View(person);
|
||||
}
|
||||
|
||||
public IActionResult JsonTextInView()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
public IActionResult ViewWithPrefixedAttributeValue()
|
||||
{
|
||||
return View();
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
@await Component.InvokeAsync("JsonTextInView")
|
||||
|
|
@ -72,7 +72,8 @@ namespace MvcSample.Web.Components
|
|||
public async Task<IViewComponentResult> InvokeAsync(int count)
|
||||
{
|
||||
var tags = await GetTagsAsync(count);
|
||||
return new JsonViewComponentResult(tags);
|
||||
|
||||
return new ContentViewComponentResult(string.Join(",", tags));
|
||||
}
|
||||
|
||||
private Task<string[]> GetTagsAsync(int count)
|
||||
|
|
|
|||
Loading…
Reference in New Issue