-
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
Razor Component IResult #47023
Conversation
src/Mvc/Mvc.ViewFeatures/src/RazorComponents/ComponentPrerenderer.cs
Outdated
Show resolved
Hide resolved
f2a26e3
to
5ce5ca6
Compare
src/Mvc/Mvc.ViewFeatures/src/Microsoft.AspNetCore.Mvc.ViewFeatures.csproj
Outdated
Show resolved
Hide resolved
a19f86e
to
4db8eb0
Compare
4db8eb0
to
5cb2bda
Compare
Don't we expect to have methods con |
/// <summary> | ||
/// Gets or sets the parameters for the component. | ||
/// </summary> | ||
public object Parameters { get; set; } |
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.
Is this meant to be an anonymously initialized object? (new { A = 1, B = 'c' }
)
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.
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.
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.
How do you unit test this action result? Not the output, but the result of a controller action that returns this result.
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.
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 😄
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.
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
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.
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
.
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.
We can make it a Dictionary<string, object>
, that will work, even if we then convert it to a ParameterView later on
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.
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.
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 public class HomeController : Controller
{
public IResult MyAction()
=> RazorComponent<MyPageComponent>();
} |
@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. |
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 |
/// <summary> | ||
/// An <see cref="IResult"/> that renders a Razor Component. | ||
/// </summary> | ||
public class RazorComponentResult : IResult |
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.
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.
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.
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. |
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 plainIResult
, not anIActionResult
or even aViewResult
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 aViewResult
, say, please let me know. @halter73 - do you have a recommendation?