-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Describe the bug
This issue is produced from the discussion in #12630
Currently in preview-7 you can embed Blazor components in an MVC/RazorPages app by using endpoints.MapBlazorHub(); and @(await Html.RenderComponentAsync<Component1>()).
However with statefull prerendering being removed in #12630, you will have to use the explicit mapping of components, like endpoints.MapBlazorHub<Counter>("counter");.
That would be ok, except that when you add multiple component registrations, the initialization code in blazor.server.js throws exceptions because it is trying to find all of the registered components in the current page/view.
How to reproduce
Consider the following scenario:
- Create a new mvc app
- Create 2 blazor components:
Component1andComponent2 - Go to /Home/Index.cshtml and add the following:
<component1>@(await Html.RenderStaticComponentAsync<Component1>())</component1>- Go to /Home/Privacy.cshtml and add the following:
<component2>@(await Html.RenderStaticComponentAsync<Component2>())</component1>- Add the blazor requirements in _Layout.cshtml:
<head>
<!-- code ommited -->
<base href="~/" />
<!-- code ommited -->
</head>
<body>
<!-- code ommited -->
<script src="_framework/blazor.server.js"></script>
</body>- Add the following to
ConfigureServicesinStartup.cs
services.AddServerSideBlazor(); - And finally, add component mapping in
Configure
endpoints.MapBlazorHub<Component1>("component1")
.AddComponent(typeof(Component2), "component2");If you try to navigate to /Home/Index or /Home/Privacy you will see the following errors in the console:
If you change the hub registration to
endpoints.MapBlazorHub();AND you change the components' rendering to
@(await Html.RenderComponentAsync<Component1>())
@(await Html.RenderComponentAsync<Component2>())
Then everything works fine (in preview-7). However this is going away, according to #12630
This fix is essential to support existing MVC and Razor Pages projects.
PS: A similar error occurs if you try to do the same in Client-Side blazor (wasm), which I have already opened an issue for in #12061, (although it got closed)

