Skip to content

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 17 commits into from
Mar 9, 2023
Merged

Conversation

SteveSandersonMS
Copy link
Member

@SteveSandersonMS SteveSandersonMS commented Mar 3, 2023

Implements #46990

There's a lot of new public API here but it's almost all boilerplate stuff following the exact patterns of PageResult, PageResultExecutor, etc.

I'm looking for feedback from MVC/Razor Pages/MinimalAPI folks on whether this is the right approach. In particular, I made RazorComponentResult be a plain IResult, not an IActionResult or even a ViewResult because we want this to work in minimal APIs, and have no requirement for MVC-like action/view concepts. If there's some reason why it should be a ViewResult, say, please let me know. @halter73 - do you have a recommendation?

@SteveSandersonMS SteveSandersonMS requested a review from a team March 3, 2023 13:19
@SteveSandersonMS SteveSandersonMS requested a review from a team as a code owner March 3, 2023 13:19
@ghost ghost added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Mar 3, 2023
@SteveSandersonMS SteveSandersonMS marked this pull request as draft March 3, 2023 13:19
@davidfowl davidfowl requested a review from DamianEdwards March 3, 2023 15:02
Base automatically changed from stevesa/ssr-as-library to main March 3, 2023 15:02
@SteveSandersonMS SteveSandersonMS force-pushed the stevesa/razorcomponent-actionresult branch from f2a26e3 to 5ce5ca6 Compare March 3, 2023 15:45
@SteveSandersonMS SteveSandersonMS added this to the 8.0-preview3 milestone Mar 6, 2023
@SteveSandersonMS SteveSandersonMS added the area-blazor Includes: Blazor, Razor Components label Mar 6, 2023
@SteveSandersonMS SteveSandersonMS self-assigned this Mar 6, 2023
@SteveSandersonMS SteveSandersonMS force-pushed the stevesa/razorcomponent-actionresult branch from a19f86e to 4db8eb0 Compare March 7, 2023 14:31
@javiercn
Copy link
Member

javiercn commented Mar 7, 2023

Don't we expect to have methods con Controller and Page to render a component?

@SteveSandersonMS SteveSandersonMS changed the title Razor Component action result Razor Component IResult Mar 7, 2023
/// <summary>
/// Gets or sets the parameters for the component.
/// </summary>
public object Parameters { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to be an anonymously initialized object? (new { A = 1, B = 'c' })

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's like what we support with Html.RenderComponentAsync - it can be an anonymously-typed object, or an IDictionary<string, object>. We could add explicit support for it being a ParameterView but I don't think many people would want to be constructing one of those.

Copy link
Member

@javiercn javiercn Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you unit test this action result? Not the output, but the result of a controller action that returns this result.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm probably not following the question, but I expected it to be the same as with any other controller action. You would have to declare the action return type as RazorComponentResult and then your unit test could inspect its properties. Or you could downcast to RazorComponentResult in the unit test. If you mean something else please clarify 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I guess that I am trying to get at is that we should probably make this a ParameterView instance and do the conversion/cast from object to it on the constructor. Otherwise, it's really hard to do anything with the parameters other than treat them as an opaque blob

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see. TBH a ParameterView isn't much easier to write assertions about - it doesn't give you random access unless you convert it to a Dictionary<string, object>

We could expose the result of PropertyHelper.ObjectToDictionary(yourValue) since we're going to perform that conversion later in order to build a ParameterView.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make it a Dictionary<string, object>, that will work, even if we then convert it to a ParameterView later on

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's what I mean with "expose the result of PropertyHelper.ObjectToDictionary(yourValue)".

Following this suggestion I've changed it and added unit tests showing how you can read the parameter values from the dictionary.

@SteveSandersonMS
Copy link
Member Author

Don't we expect to have methods con Controller and Page to render a component?

I'm totally fine with adding that but was hoping for feedback from some of the MVC-type people. At least in the MVC controller sense it makes sense to me, maybe a bit less so for a Razor page.

As it stands, it's already easy to use from an MVC controller because you just do:

public class HomeController : Controller
{
    public IResult MyAction()
        => new RazorComponentResult<MyPageComponent>();
}

Whether there's any clear merit to also adding a helper method on Controller I don't know. The usage experience would be very similar:

public class HomeController : Controller
{
    public IResult MyAction()
        => RazorComponent<MyPageComponent>();
}

@SteveSandersonMS SteveSandersonMS marked this pull request as ready for review March 7, 2023 17:24
@javiercn
Copy link
Member

javiercn commented Mar 7, 2023

@SteveSandersonMS I think we should have at least a method on Controller to do render it, I do not believe we have any action result that we tell users to instantiate manually.

@SteveSandersonMS
Copy link
Member Author

What about for Minimal APIs? Are minimal people OK with instantiating their own result objects? If not I think I need to put this in the Microsoft.AspNetCore.Http.Results project, which is awkward because then I have to make that reference Mvc.ViewFeatures, or we have to do some bigger refactoring. I don't think we should hang it off Results.Extensions - that would be super weird for a built-in feature. cc @captainsafia

@SteveSandersonMS
Copy link
Member Author

@javiercn Adding the helpers for constructing a RazorComponentResult is the sort of detail we talked about that can be filled in later. Since we don't yet have any feedback from Minimal/MVC people here, I've filed #47094 and suggest we proceed with the more basic low-level APIs in this PR for now.

/// <summary>
/// An <see cref="IResult"/> that renders a Razor Component.
/// </summary>
public class RazorComponentResult : IResult
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, I am not 100% sure how I feel about automatically rendering the Layout for a component, or if there should be a way to override that choice.

I believe MVC has functionality to change the layout or signal that you don't want the layout, maybe we should consider having similar functionality.

@SteveSandersonMS
Copy link
Member Author

Adding the ability to override the layout would be fine and easy for us to add later. Strictly speaking there are other ways to do this anyway, but if enough people want it then we could make it more convenient.

@SteveSandersonMS SteveSandersonMS merged commit 5b70808 into main Mar 9, 2023
@SteveSandersonMS SteveSandersonMS deleted the stevesa/razorcomponent-actionresult branch March 9, 2023 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants