sanepete
sanepete

Reputation: 1128

How can I make .net 8.0 razor components, in a shared project, interactive?

I have a visual studio solution containing several blazor web application projects. Each of the applications has a requirement to use several identical components. I should like to place web assembly components in a shared project, add them as project references and include them in the _imports.razor file and include them on the pages of the various web applications.

I have placed the 'counter' component in the shared project and added it to a page:

<Counter @rendermode="InteractiveAuto"/>

But running the project gives me the following error:

InvalidOperationException: A component of type 'myawesomenamespace.Counter' has render mode 'InteractiveAutoRenderMode', but the required endpoints are not mapped on the server. When calling 'MapRazorComponents', add a call to 'AddInteractiveServerRenderMode'. For example, 'builder.MapRazorComponents<...>.AddInteractiveServerRenderMode()'

This line:

app.MapRazorComponents<web.components.App>()
 .AddInteractiveWebAssemblyRenderMode()
 .AddAdditionalAssemblies(typeof(wasm._imports).Assembly);

is present in the server Program.cs file.

If I exclude the '@rendermode="InteractiveAuto"' directive, the component renders but the button doesn't work.

Are interactive components in a shared project even possible?

Upvotes: 0

Views: 687

Answers (2)

sanepete
sanepete

Reputation: 1128

In the web project Program.cs, adds the line:

<Counter @rendermode="InteractiveWebAssembly"/>

Making all the shared components downloadable web assemblies suits the purposes of the project very well.

Upvotes: 0

Ben
Ben

Reputation: 191

In my case, adding this line to app.razor under the body tag:

<Routes @rendermode="InteractiveServer" />

will apply to all components in your project.

Upvotes: 0

Related Questions