-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
RazorComponentResult returned component cannot execute clientside code #50762
Comments
You can't serve an interactive component without a static page in which to host it. That is why the new SSR model requires you to have the root And I'm not sure that interactive components are supported for component endpoints anyway. But theoretically: To enable interactivity, you need to serve the But you could check the source code, and look for the extension method with code that adds those static files to be hosted. I think it is in |
Hi |
@michaelongithub I see. You try to create an MVC style app with Razor Components for views in custom endpoints. I don't think that it is a supported case. Component endpoints are not meant to allow you do create MVC-style apps like with "Razor Views" in MVC. Btw. Another thing to consider is the use of layouts for shared UI. They will not work unless you have the I do however agree that there are some things with component endpoints that aren't clear. |
@marinasundstrom Thanks again. So my understanding of the current situation is as follows:
|
@michaelongithub Btw. I now see that you are using https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-7.0 Have you tried using the |
@danroth27 Are interactive components supported through Minimal API endpoints returning ComponentResult? (Apart from the limitation that you would have to render them in a SSR component) |
@marinasundstrom Thanks again for the hint And now per your advice
both render but make no clientside code running. |
Have you included the script? (Provided that you run .NET 8) <script src="_framework/blazor.web.js" suppress-error="BL9992"></script> I checked, and the Blazor Server template uses: <script src="_framework/blazor.server.js"></script> That is required to activate the interactivity. So it should be either directly in your view (.cshtml), or in a layout/master file. |
@michaelongithub I have created a functioning MVC app that has a view that renders an interactive Razor component. To me it seem like it is working in .NET 8. https://github.com/marinasundstrom/BlazorMvcViewTest The route is Edit: I also tested creating a component endpoint. It is all using the normal Blazor Server stuff. |
@marinasundstrom Tested Your Example:
|
@michaelongithub Yes. I created a Blazor Server .NET 7 project, and upgraded it. And true, I did not use the new script. In the case of the The issue is that Blazor only exposes And I reckon that Blazor Server stuff is doing similar stuff as those extension methods under the hood. Because the script is not being exposed, I asked @danroth27 for answers whether interactive components are supported in components from plain endpoints. I have update the test app, and added a custom That way you can reference and use the newer script. But you will not be able to use Blazor WASM components without adding them to the container first - I think. @SteveSandersonMS Perhaps you know the details of this? How to approach this scenario. |
Thanks for contacting us. We're moving this issue to the |
I have tested Minimal API endpoint with interactive components. It works. https://github.com/marinasundstrom/BlazorMinimalApiTest Created components:
Register Razor component services: builder.Services.AddRazorComponents()
.AddServerComponents(); Serve the Blazor JS script: app.UseBlazorWebJS() Map endpoint: app.MapGet("/test", () => new RazorComponentResult<BlazorMinimalApi.Components.MyPage>()); Map Blazor Hub: app.MapBlazorHub(); I don't know how well this is documented. But then, the script is not exposed other than through the offical API. |
@marinasundstrom Amazing. Admirable. Shows that it is basically possible in .net 8.
|
@michaelongithub I'm just a humble enthusiast and developer who is happy to help. And it was much needed distraction to me! 🙂 I guess it is about priorities. Now when the final release is near. When I started experimenting with this, a month ago, I noticed that the blazor.web.js wasn't served unless you called MapRazorComponents(). So I dug into the code and extracted that bit into my own extension method. The key to make interactive server components work is the .MapBlazorHub(), as well as serving the script. I think that the Blazor team needs time to think about how to design these APIs. In the meantime, you have this workaround. Which I documented about in my repository for others to see. But I have not tried out WebAssembly. I guess that I need to expose more static files. I will update once I have explored that. |
@marinasundstrom Thanks for letting me know. Currently we have not used and short term foreseeably don't intent to use WASM. So I will wait and see, what the team will come up with in RC2 and RTM. I hope they will clarify and document the state of affairs with regard the topics we discussed and You researched. And your distraction from whatever was at any rate very helpful for me, to see what the current state is with regard to my initial motivation. |
FYI. I got WebAssembly working as well. So added endpoint for that. https://github.com/marinasundstrom/BlazorMinimalApiTest/blob/main/README.md |
@marinasundstrom Indefatigable ! From the viewpoint of a wide perspective of a "Blazor Fullstack" I would expect the possibility to return a fully functional Blazor Component programmatically in a very straightforward way. My main argument being: You can return Razor Views and Pages (from Controllers). So please make that possible for Blazor Components. For me personally the primary concern is not minimal API's but a mvc application architecture. |
@michaelongithub You have to know that endpoints that serve Razor Components aren't a replacement for Razor Views in MVC. You will not get the same with Razor components. You won't get things like layouts since they require you to have a And I don't know if enhanced navigation, and such will work in your MVC case. So in normal cases, when you are building full on interactive Blazor apps, you would rather use the other way of letting ASP.NET Core registering the pages to be used by the router. But I think that since ASP.NET now supports rendering Blazor components through endpoints, and that you can add interactivity, then the functionality should be exposed so that developers can use it. |
@mkArtakMSFT What should we do with this? |
I am going to clarify the state of things for .NET 8.0
With this in mind, very likely our focus moving forward will be to continue expanding on the Blazor Web experience (MapRazorComponents), as that is the mainline scenario for us. |
@javiercn: Thanks a lot for this clarification |
Hi
I want to return a Razor Component from a controller. (.net 8 RC1, newest Blazor WebApp Template)
I have a simple test component (the infamous Counter.razor) with @Attribute [RenderModeServer]
Returns correct rendered component But counter event is not triggered:
BTW: The same behaviour if I use the component inside a Razor page (cshtml) with @await Component.InvokeAsync("Counter");
So has this maybe to do with that these calls cannot handle the new @Attribute [RenderModeServer] ?
Thanks for information, Michael
The text was updated successfully, but these errors were encountered: