From 096007b394019f24586deb066b09a56258960774 Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Mon, 1 Feb 2016 14:55:52 -0800 Subject: [PATCH] Updated `Controller`s `ViewComponent` method to handle arguments. - This bit was missed when changing the `ViewComponent` invocation pattern resulting in the inability to invoke `ViewComponent`s with arguments in a `Controller`. #4004 --- .../Controller.cs | 20 +++++++++++----- .../IViewComponentHelper.cs | 4 ++-- .../BasicTests.cs | 24 +++++++++++++++++++ .../BasicWebSite.PassThrough.Index.html | 1 + .../Components/PassThroughViewComponent.cs | 15 ++++++++++++ .../Controllers/PassThroughController.cs | 16 +++++++++++++ .../Components/PassThrough/Default.cshtml | 3 +++ 7 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/BasicWebSite.PassThrough.Index.html create mode 100644 test/WebSites/BasicWebSite/Components/PassThroughViewComponent.cs create mode 100644 test/WebSites/BasicWebSite/Controllers/PassThroughController.cs create mode 100644 test/WebSites/BasicWebSite/Views/Shared/Components/PassThrough/Default.cshtml diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Controller.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Controller.cs index a680911688..d71833454e 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Controller.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Controller.cs @@ -216,12 +216,16 @@ namespace Microsoft.AspNetCore.Mvc /// The view component name. Can be a view component /// or /// . - /// The arguments to pass to the view component. + /// + /// An anonymous containing arguments to be passed to the invoked view component method. + /// Alternatively, an instance containing + /// the invocation arguments. + /// /// The created object for the response. [NonAction] - public virtual ViewComponentResult ViewComponent(string componentName, params object[] arguments) + public virtual ViewComponentResult ViewComponent(string componentName, object arguments) { - return new ViewComponentResult() + return new ViewComponentResult { ViewComponentName = componentName, Arguments = arguments, @@ -235,12 +239,16 @@ namespace Microsoft.AspNetCore.Mvc /// render. /// /// The view component . - /// The arguments to pass to the view component. + /// + /// An anonymous containing arguments to be passed to the invoked view component method. + /// Alternatively, an instance containing + /// the invocation arguments. + /// /// The created object for the response. [NonAction] - public virtual ViewComponentResult ViewComponent(Type componentType, params object[] arguments) + public virtual ViewComponentResult ViewComponent(Type componentType, object arguments) { - return new ViewComponentResult() + return new ViewComponentResult { ViewComponentType = componentType, Arguments = arguments, diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/IViewComponentHelper.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/IViewComponentHelper.cs index 26e470fc3b..8bfcc4ce16 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/IViewComponentHelper.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/IViewComponentHelper.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Mvc /// /// The name of the view component. /// - /// An containing arguments to be passed to the invoked view component method. + /// An anonymous containing arguments to be passed to the invoked view component method. /// Alternatively, an instance containing /// the invocation arguments. /// @@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Mvc /// /// The view component . /// - /// An containing arguments to be passed to the invoked view component method. + /// An anonymous containing arguments to be passed to the invoked view component method. /// Alternatively, an instance containing /// the invocation arguments. /// diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs index 82db451418..66cdeda292 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs @@ -30,6 +30,30 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public HttpClient Client { get; } + [Fact] + public async Task CanRender_ViewComponentWithArgumentsFromController() + { + // Arrange + var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); + var outputFile = "compiler/resources/BasicWebSite.PassThrough.Index.html"; + var expectedContent = + await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false); + + // Act + var response = await Client.GetAsync("PassThrough/Index?value=123"); + var responseContent = await response.Content.ReadAsStringAsync(); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); + +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true); +#endif + } + [Theory] [InlineData("")] [InlineData("Home")] diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/BasicWebSite.PassThrough.Index.html b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/BasicWebSite.PassThrough.Index.html new file mode 100644 index 0000000000..6cec33b91a --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/BasicWebSite.PassThrough.Index.html @@ -0,0 +1 @@ +

The passed through value was: 123

\ No newline at end of file diff --git a/test/WebSites/BasicWebSite/Components/PassThroughViewComponent.cs b/test/WebSites/BasicWebSite/Components/PassThroughViewComponent.cs new file mode 100644 index 0000000000..7af257e126 --- /dev/null +++ b/test/WebSites/BasicWebSite/Components/PassThroughViewComponent.cs @@ -0,0 +1,15 @@ +// 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 Microsoft.AspNetCore.Mvc; + +namespace HtmlGenerationWebSite.Components +{ + public class PassThroughViewComponent : ViewComponent + { + public IViewComponentResult Invoke(long value) + { + return View(value); + } + } +} diff --git a/test/WebSites/BasicWebSite/Controllers/PassThroughController.cs b/test/WebSites/BasicWebSite/Controllers/PassThroughController.cs new file mode 100644 index 0000000000..a1359ce5a2 --- /dev/null +++ b/test/WebSites/BasicWebSite/Controllers/PassThroughController.cs @@ -0,0 +1,16 @@ +// 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 HtmlGenerationWebSite.Components; +using Microsoft.AspNetCore.Mvc; + +namespace BasicWebSite.Controllers +{ + public class PassThroughController : Controller + { + public IActionResult Index(long value) + { + return ViewComponent(typeof(PassThroughViewComponent), new { value }); + } + } +} diff --git a/test/WebSites/BasicWebSite/Views/Shared/Components/PassThrough/Default.cshtml b/test/WebSites/BasicWebSite/Views/Shared/Components/PassThrough/Default.cshtml new file mode 100644 index 0000000000..87248ac689 --- /dev/null +++ b/test/WebSites/BasicWebSite/Views/Shared/Components/PassThrough/Default.cshtml @@ -0,0 +1,3 @@ +@model long + +

The passed through value was: @Model

\ No newline at end of file