diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponent.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponent.cs index 3736b3c94c..5315038586 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponent.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponent.cs @@ -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); } - /// - /// Returns a result which will render JSON text. - /// - /// The value to output in JSON text. - /// A . - public JsonViewComponentResult Json(object value) - { - return new JsonViewComponentResult(value); - } - - /// - /// Returns a result which will render JSON text. - /// - /// The value to output in JSON text. - /// The to be used by - /// the formatter. - /// A . - /// Callers should cache an instance of to avoid - /// recreating cached data with each call. - public JsonViewComponentResult Json(object value, JsonSerializerSettings serializerSettings) - { - if (serializerSettings == null) - { - throw new ArgumentNullException(nameof(serializerSettings)); - } - - return new JsonViewComponentResult(value, serializerSettings); - } - /// /// Returns a result which will render the partial view with name "Default". /// diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/JsonViewComponentResult.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/JsonViewComponentResult.cs deleted file mode 100644 index 8a5e37c8b5..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/JsonViewComponentResult.cs +++ /dev/null @@ -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 -{ - /// - /// An which renders JSON text when executed. - /// - public class JsonViewComponentResult : IViewComponentResult - { - private readonly JsonSerializerSettings _serializerSettings; - - /// - /// Initializes a new . - /// - /// The value to format as JSON text. - public JsonViewComponentResult(object value) - { - Value = value; - } - - /// - /// Initializes a new . - /// - /// The value to format as JSON text. - /// The to be used by - /// the formatter. - public JsonViewComponentResult(object value, JsonSerializerSettings serializerSettings) - { - if (serializerSettings == null) - { - throw new ArgumentNullException(nameof(serializerSettings)); - } - - Value = value; - _serializerSettings = serializerSettings; - } - - /// - /// Gets the value. - /// - public object Value { get; } - - /// - /// Renders JSON text to the output. - /// - /// The . - 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>() - .Value - .SerializerSettings; - } - - using (var jsonWriter = new JsonTextWriter(context.Writer)) - { - jsonWriter.CloseOutput = false; - var jsonSerializer = JsonSerializer.Create(serializerSettings); - jsonSerializer.Serialize(jsonWriter, Value); - } - } - - /// - /// Renders JSON text to the output. - /// - /// The . - /// A completed . - public Task ExecuteAsync(ViewComponentContext context) - { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - Execute(context); - return Task.FromResult(true); - } - } -} diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs index 66cdeda292..de5b720615 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs @@ -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() { diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Index.html b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Index.html index fce8ef52b7..dcf5fd12da 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Index.html +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Index.html @@ -33,11 +33,11 @@

Current Tag Cloud from Tag Helper

-
["Lorem","ipsum","dolor","sit","amet","consectetur","adipisicing","elit","sed","do","eiusmod","tempor","incididunt","ut","labore","et","dolore","magna","aliquaUt","enim"]
+
Lorem,ipsum,dolor,sit,amet,consectetur,adipisicing,elit,sed,do,eiusmod,tempor,incididunt,ut,labore,et,dolore,magna,aliquaUt,enim

Current Tag Cloud from ViewComponentHelper:

-
["Lorem","ipsum","dolor","sit","amet","consectetur","adipisicing","elit","sed","do","eiusmod","tempor","incididunt","ut","labore"]
+
Lorem,ipsum,dolor,sit,amet,consectetur,adipisicing,elit,sed,do,eiusmod,tempor,incididunt,ut,labore

Rendering Template:

-

Tag Cloud from Template:

["Lorem","ipsum","dolor","sit","amet","consectetur","adipisicing","elit","sed","do","eiusmod","tempor","incididunt","ut","labore","et","dolore","magna","aliquaUt","enim"]
+

Tag Cloud from Template:

Lorem,ipsum,dolor,sit,amet,consectetur,adipisicing,elit,sed,do,eiusmod,tempor,incididunt,ut,labore,et,dolore,magna,aliquaUt,enim

diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponentTests.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponentTests.cs index f1f04c0a42..7de932ba39 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponentTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponentTests.cs @@ -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(actualResult); - Assert.Same(testData, actualResult.Value); - } - [Fact] public void ViewComponent_View_WithEmptyParameter_SetsResultViewWithDefaultViewName() { diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/JsonViewComponentResultTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/JsonViewComponentResultTest.cs deleted file mode 100644 index 096c046b0a..0000000000 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/JsonViewComponentResultTest.cs +++ /dev/null @@ -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(); - 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(); - 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(), - 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(), - 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; - } - } -} diff --git a/test/WebSites/BasicWebSite/Components/JsonTextInView.cs b/test/WebSites/BasicWebSite/Components/JsonTextInView.cs deleted file mode 100644 index 90cdc3a513..0000000000 --- a/test/WebSites/BasicWebSite/Components/JsonTextInView.cs +++ /dev/null @@ -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" - }); - } - } -} \ No newline at end of file diff --git a/test/WebSites/BasicWebSite/Controllers/HomeController.cs b/test/WebSites/BasicWebSite/Controllers/HomeController.cs index ffe5ebb0ab..ad65b93771 100644 --- a/test/WebSites/BasicWebSite/Controllers/HomeController.cs +++ b/test/WebSites/BasicWebSite/Controllers/HomeController.cs @@ -83,11 +83,6 @@ namespace BasicWebSite.Controllers return View(person); } - public IActionResult JsonTextInView() - { - return View(); - } - public IActionResult ViewWithPrefixedAttributeValue() { return View(); diff --git a/test/WebSites/BasicWebSite/Views/Home/JsonTextInView.cshtml b/test/WebSites/BasicWebSite/Views/Home/JsonTextInView.cshtml deleted file mode 100644 index 1424208b13..0000000000 --- a/test/WebSites/BasicWebSite/Views/Home/JsonTextInView.cshtml +++ /dev/null @@ -1 +0,0 @@ -@await Component.InvokeAsync("JsonTextInView") \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/TagHelpers/TagCloudViewComponentTagHelper.cs b/test/WebSites/TagHelpersWebSite/TagHelpers/TagCloudViewComponentTagHelper.cs index 817a794553..50c86e7e25 100644 --- a/test/WebSites/TagHelpersWebSite/TagHelpers/TagCloudViewComponentTagHelper.cs +++ b/test/WebSites/TagHelpersWebSite/TagHelpers/TagCloudViewComponentTagHelper.cs @@ -72,7 +72,8 @@ namespace MvcSample.Web.Components public async Task InvokeAsync(int count) { var tags = await GetTagsAsync(count); - return new JsonViewComponentResult(tags); + + return new ContentViewComponentResult(string.Join(",", tags)); } private Task GetTagsAsync(int count)