This commit is contained in:
Pranav K 2019-10-11 13:46:45 -07:00
parent 3fe0e303c6
commit 79a1769e47
No known key found for this signature in database
GPG Key ID: F748807460A27E91
4 changed files with 27 additions and 6 deletions

View File

@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests
} }
[Fact] [Fact]
public void PassingParametersToComponentsWorks() public void PassingParametersToComponentsFromThePageWorks()
{ {
Navigate("/prerendered/componentwithparameters?QueryValue=testQueryValue"); Navigate("/prerendered/componentwithparameters?QueryValue=testQueryValue");

View File

@ -10,8 +10,8 @@
</head> </head>
<body> <body>
<div id="test-container"> <div id="test-container">
<component type="typeof(GreeterComponent)" render-mode="ServerPrerendered" /> @(await Html.RenderComponentAsync<GreeterComponent>(RenderMode.ServerPrerendered))
<component type="typeof(GreeterComponent)" render-mode="Server" /> @(await Html.RenderComponentAsync<GreeterComponent>(RenderMode.Server))
<component type="typeof(GreeterComponent)" render-mode="Static" param-name='"John"' /> <component type="typeof(GreeterComponent)" render-mode="Static" param-name='"John"' />
<component type="typeof(GreeterComponent)" render-mode="Server"/> <component type="typeof(GreeterComponent)" render-mode="Server"/>
<div id="container"> <div id="container">

View File

@ -324,6 +324,7 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
} }
public static partial class HtmlHelperComponentExtensions public static partial class HtmlHelperComponentExtensions
{ {
public static System.Threading.Tasks.Task<Microsoft.AspNetCore.Html.IHtmlContent> RenderComponentAsync(this Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper htmlHelper, System.Type componentType, Microsoft.AspNetCore.Mvc.Rendering.RenderMode renderMode, object parameters) { throw null; }
public static System.Threading.Tasks.Task<Microsoft.AspNetCore.Html.IHtmlContent> RenderComponentAsync<TComponent>(this Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper htmlHelper, Microsoft.AspNetCore.Mvc.Rendering.RenderMode renderMode) where TComponent : Microsoft.AspNetCore.Components.IComponent { throw null; } public static System.Threading.Tasks.Task<Microsoft.AspNetCore.Html.IHtmlContent> RenderComponentAsync<TComponent>(this Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper htmlHelper, Microsoft.AspNetCore.Mvc.Rendering.RenderMode renderMode) where TComponent : Microsoft.AspNetCore.Components.IComponent { throw null; }
public static System.Threading.Tasks.Task<Microsoft.AspNetCore.Html.IHtmlContent> RenderComponentAsync<TComponent>(this Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper htmlHelper, Microsoft.AspNetCore.Mvc.Rendering.RenderMode renderMode, object parameters) where TComponent : Microsoft.AspNetCore.Components.IComponent { throw null; } public static System.Threading.Tasks.Task<Microsoft.AspNetCore.Html.IHtmlContent> RenderComponentAsync<TComponent>(this Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper htmlHelper, Microsoft.AspNetCore.Mvc.Rendering.RenderMode renderMode, object parameters) where TComponent : Microsoft.AspNetCore.Components.IComponent { throw null; }
} }

View File

@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
public static class HtmlHelperComponentExtensions public static class HtmlHelperComponentExtensions
{ {
/// <summary> /// <summary>
/// Renders the <typeparamref name="TComponent"/> <see cref="IComponent"/>. /// Renders the <typeparamref name="TComponent"/>.
/// </summary> /// </summary>
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/>.</param> /// <param name="htmlHelper">The <see cref="IHtmlHelper"/>.</param>
/// <param name="renderMode">The <see cref="RenderMode"/> for the component.</param> /// <param name="renderMode">The <see cref="RenderMode"/> for the component.</param>
@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
=> RenderComponentAsync<TComponent>(htmlHelper, renderMode, parameters: null); => RenderComponentAsync<TComponent>(htmlHelper, renderMode, parameters: null);
/// <summary> /// <summary>
/// Renders the <typeparamref name="TComponent"/> <see cref="IComponent"/>. /// Renders the <typeparamref name="TComponent"/>.
/// </summary> /// </summary>
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/>.</param> /// <param name="htmlHelper">The <see cref="IHtmlHelper"/>.</param>
/// <param name="parameters">An <see cref="object"/> containing the parameters to pass /// <param name="parameters">An <see cref="object"/> containing the parameters to pass
@ -36,15 +36,35 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
this IHtmlHelper htmlHelper, this IHtmlHelper htmlHelper,
RenderMode renderMode, RenderMode renderMode,
object parameters) where TComponent : IComponent object parameters) where TComponent : IComponent
=> RenderComponentAsync(htmlHelper, typeof(TComponent), renderMode, parameters);
/// <summary>
/// Renders the specified <paramref name="componentType"/>.
/// </summary>
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/>.</param>
/// <param name="componentType">The component type.</param>
/// <param name="parameters">An <see cref="object"/> containing the parameters to pass
/// to the component.</param>
/// <param name="renderMode">The <see cref="RenderMode"/> for the component.</param>
public static Task<IHtmlContent> RenderComponentAsync(
this IHtmlHelper htmlHelper,
Type componentType,
RenderMode renderMode,
object parameters)
{ {
if (htmlHelper is null) if (htmlHelper is null)
{ {
throw new ArgumentNullException(nameof(htmlHelper)); throw new ArgumentNullException(nameof(htmlHelper));
} }
if (componentType is null)
{
throw new ArgumentNullException(nameof(componentType));
}
var viewContext = htmlHelper.ViewContext; var viewContext = htmlHelper.ViewContext;
var componentRenderer = viewContext.HttpContext.RequestServices.GetRequiredService<IComponentRenderer>(); var componentRenderer = viewContext.HttpContext.RequestServices.GetRequiredService<IComponentRenderer>();
return componentRenderer.RenderComponentAsync(viewContext, typeof(TComponent), renderMode, parameters); return componentRenderer.RenderComponentAsync(viewContext, componentType, renderMode, parameters);
} }
} }
} }