-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Razor Component IResult #47023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Razor Component IResult #47023
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c12ea92
Initial RazorComponentResult implementation
SteveSandersonMS 92d5325
Support layouts and anonymously-typed parameter objects
SteveSandersonMS d5fa2c0
Update RazorComponentResultHost.cs
SteveSandersonMS 8770ebc
Fix other render modes
SteveSandersonMS 793f88f
Add Results.Extensions helper
SteveSandersonMS 5cb2bda
Revert "Add Results.Extensions helper"
SteveSandersonMS 2c0452b
Fix unrelated build issue from different work item
SteveSandersonMS 57638d2
Add RazorComponentResult<TComponent>
SteveSandersonMS 71e6b82
Make clear which render mode is supported currently
SteveSandersonMS 7b3d4b0
Add unit tests
SteveSandersonMS cc733e6
Make it an IResult rather than IActionResult
SteveSandersonMS 314173c
More testing
SteveSandersonMS 8b91b7c
Expose parameters as dictionary for unit tests
SteveSandersonMS 7daf0aa
Test fixes
SteveSandersonMS 83b3e61
Fix XML docs
SteveSandersonMS 4a53208
Clean up some unnecessary stuff based on what's coming in later work
SteveSandersonMS 3148d0f
A bit more cleanup
SteveSandersonMS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Mvc/Mvc.ViewFeatures/src/MvcViewFeaturesDiagnosticListenerExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,24 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Mvc.Rendering.FormMethod.Dialog = 2 -> Microsoft.AspNetCore.Mvc.Rendering.FormMethod | ||
Microsoft.AspNetCore.Mvc.Rendering.FormMethod.Dialog = 2 -> Microsoft.AspNetCore.Mvc.Rendering.FormMethod | ||
Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult | ||
Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult<TComponent>.RazorComponentResult() -> void | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult.ExecuteAsync(Microsoft.AspNetCore.Http.HttpContext httpContext) -> System.Threading.Tasks.Task | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult.Parameters.get -> System.Collections.Generic.IReadOnlyDictionary<string, object> | ||
Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult.RenderMode.get -> Microsoft.AspNetCore.Mvc.Rendering.RenderMode | ||
Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult.RenderMode.set -> void | ||
Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult.StatusCode.get -> int? | ||
Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult.StatusCode.set -> void | ||
Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResultExecutor | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult.ComponentType.get -> System.Type | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult.ContentType.get -> string | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult.ContentType.set -> void | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult.RazorComponentResult(System.Type componentType) -> void | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult.RazorComponentResult(System.Type componentType, object parameters) -> void | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult.RazorComponentResult(System.Type componentType, System.Collections.Generic.IReadOnlyDictionary<string, object> parameters) -> void | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult<TComponent> | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult<TComponent>.RazorComponentResult(object parameters) -> void | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult<TComponent>.RazorComponentResult(System.Collections.Generic.IReadOnlyDictionary<string, object> parameters) -> void | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResultExecutor.RazorComponentResultExecutor(Microsoft.AspNetCore.Mvc.Infrastructure.IHttpResponseStreamWriterFactory writerFactory) -> void | ||
~Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResultExecutor.WriterFactory.get -> Microsoft.AspNetCore.Mvc.Infrastructure.IHttpResponseStreamWriterFactory | ||
~static readonly Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResultExecutor.DefaultContentType -> string | ||
~virtual Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResultExecutor.ExecuteAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.AspNetCore.Mvc.ViewFeatures.RazorComponentResult result) -> System.Threading.Tasks.Task |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
.../src/RazorComponents/IAsyncHtmlContent.cs → .../src/RazorComponents/IHtmlAsyncContent.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Html; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ViewFeatures; | ||
|
||
// For prerendered components, we can't use IHtmlComponent directly because it has no asynchrony and | ||
// hence can't dispatch to the renderer's sync context. | ||
internal interface IAsyncHtmlContent | ||
internal interface IHtmlAsyncContent : IHtmlContent | ||
{ | ||
ValueTask WriteToAsync(TextWriter writer); | ||
} |
96 changes: 96 additions & 0 deletions
96
src/Mvc/Mvc.ViewFeatures/src/RazorComponents/RazorComponentResult.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ViewFeatures; | ||
|
||
/// <summary> | ||
/// An <see cref="IResult"/> that renders a Razor Component. | ||
/// </summary> | ||
public class RazorComponentResult : IResult | ||
{ | ||
private static readonly IReadOnlyDictionary<string, object> EmptyParameters | ||
= new Dictionary<string, object>().AsReadOnly(); | ||
|
||
/// <summary> | ||
/// Constructs an instance of <see cref="RazorComponentResult"/>. | ||
/// </summary> | ||
/// <param name="componentType">The type of the component to render. This must implement <see cref="IComponent"/>.</param> | ||
public RazorComponentResult(Type componentType) | ||
: this(componentType, null) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Constructs an instance of <see cref="RazorComponentResult"/>. | ||
/// </summary> | ||
/// <param name="componentType">The type of the component to render. This must implement <see cref="IComponent"/>.</param> | ||
/// <param name="parameters">Parameters for the component.</param> | ||
public RazorComponentResult(Type componentType, object parameters) | ||
: this(componentType, CoerceParametersObjectToDictionary(parameters)) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Constructs an instance of <see cref="RazorComponentResult"/>. | ||
/// </summary> | ||
/// <param name="componentType">The type of the component to render. This must implement <see cref="IComponent"/>.</param> | ||
/// <param name="parameters">Parameters for the component.</param> | ||
public RazorComponentResult(Type componentType, IReadOnlyDictionary<string, object> parameters) | ||
{ | ||
// Note that the Blazor renderer will validate that componentType implements IComponent and throws a suitable | ||
// exception if not, so we don't need to duplicate that logic here. | ||
|
||
ArgumentNullException.ThrowIfNull(componentType); | ||
ComponentType = componentType; | ||
Parameters = parameters ?? EmptyParameters; | ||
} | ||
|
||
private static IReadOnlyDictionary<string, object> CoerceParametersObjectToDictionary(object parameters) | ||
=> parameters is null | ||
? null | ||
: (IReadOnlyDictionary<string, object>)PropertyHelper.ObjectToDictionary(parameters); | ||
|
||
/// <summary> | ||
/// Gets the component type. | ||
/// </summary> | ||
public Type ComponentType { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets the Content-Type header for the response. | ||
/// </summary> | ||
public string ContentType { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the HTTP status code. | ||
/// </summary> | ||
public int? StatusCode { get; set; } | ||
|
||
/// <summary> | ||
/// Gets the parameters for the component. | ||
/// </summary> | ||
public IReadOnlyDictionary<string, object> Parameters { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets the rendering mode. | ||
/// </summary> | ||
public RenderMode RenderMode { get; set; } = RenderMode.Static; | ||
|
||
/// <summary> | ||
/// Requests the service of | ||
/// <see cref="RazorComponentResultExecutor.ExecuteAsync(HttpContext, RazorComponentResult)" /> | ||
/// to process itself in the given <paramref name="httpContext" />. | ||
/// </summary> | ||
/// <param name="httpContext">An <see cref="HttpContext" /> associated with the current request.</param > | ||
/// <returns >A <see cref="T:System.Threading.Tasks.Task" /> which will complete when execution is completed.</returns > | ||
public Task ExecuteAsync(HttpContext httpContext) | ||
{ | ||
var executor = httpContext.RequestServices.GetRequiredService<RazorComponentResultExecutor>(); | ||
return executor.ExecuteAsync(httpContext, this); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we have changed this to only implement
IResult
and not IActionResult, does it run through the MVC result filters?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, MVC runs result filters around
IResult
results.