conficient / BlazorTemplater

A library that generates HTML (e.g. for emails) from Razor Components
Apache License 2.0
146 stars 16 forks source link

Cannot supply a component of type 'Components.BookComponent' because the current platform does not support the render mode 'Microsoft.AspNetCore.Components.Web.InteractiveServerRenderMode' #38

Closed MahdiElahi closed 3 months ago

MahdiElahi commented 3 months ago

when i call component by this way i have this error

in TestComponent.razor i call BookComponent to get html var sensorHtml = new ComponentRenderer() .Render();

BookComponent.razor

@rendermode InteractiveServer other codes......

but i have this error

Cannot supply a component of type 'Components.BookComponent' because the current platform does not support the render mode 'Microsoft.AspNetCore.Components.Web.InteractiveServerRenderMode'

conficient commented 3 months ago

The Render Modes in .NET 8 are not supported in component rendering to HTML strings.

These modes are used to control how controls interact with the end user, and this is not required when using component rendering to a string where no interactivity is possible. If the component is not used in the UI you should remove the @rendermode statement - it has no value for HTML rendering.

As an experiment I tested this with .NET 8's own HTML rendering system and it generated a similar error. So .NET 8 doesn't support this either.

Workaround

If you're re-using a UI component, and really need to keep the @rendermode, perhaps split the component into two parts: an outer component with the render mode attribute, and contains the inner component. BookComponent.razor

@rendermode InteractiveServer
<BookComponent2 />

BookComponent2.razor

<div>..book component</div>

You'd use BookComponent2 for the HTML string render. Hope this helps.