Skip to content

[Blazor] Multiple entry-point components in MVC/RazorPages apps does not work #12795

@stavroskasidis

Description

@stavroskasidis

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:

  1. Create a new mvc app
  2. Create 2 blazor components: Component1 and Component2
  3. Go to /Home/Index.cshtml and add the following:
<component1>@(await Html.RenderStaticComponentAsync<Component1>())</component1>
  1. Go to /Home/Privacy.cshtml and add the following:
<component2>@(await Html.RenderStaticComponentAsync<Component2>())</component1>
  1. 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>
  1. Add the following to ConfigureServices in Startup.cs
    services.AddServerSideBlazor();
  2. 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:

Home.cshtml
image

Privacy.cshtml
image

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)

Metadata

Metadata

Assignees

Labels

DoneThis issue has been fixedarea-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing one

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions