最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Blazor rendering - joining multiple MarkupStrings together with unclosed HTML elements - Stack Overflow

programmeradmin1浏览0评论

Im curently rewriting old WebForms app to Blazor InteractiveServerApp (.NET 9). This app is reading and processing static template ".htm" files, that contains partial HTML markups and custom "internal" markups. For example how that file can look like:

MyCustomTemplate.htm

<table>
  <tr>
    <td>
      [MySpecialTagToBeTranslated]
    </td>
   </tr>
</table>

The idea is to give customers (implementation partners) ability to change the frontend freely as they need. The custom markup is then used to hide the complex code and binding logic behind "simpler" tag.

So, after processing that template, I have something like this:

ProcessingControl.razor

@((MarkupString)"<table><tr><td>")

@RenderProcessedControl (translates to "<label>My processed content</label>")

@((MarkupString)"</td></tr></table>")

@code {
// RenderFragment to build the controls
private RenderFragment RenderProcessedControl() => builder =>
{
    builder.OpenComponent<MySpecialTagToBeTranslated>(0);
    builder.CloseComponent();
    ... etc
};
}

The idea in this example is to have "My processed content" from component inside "" tag. But the final result in browser is:

<table><tr><td>

</td></tr></table>

<label>My processed content</label>

Is there a solution that I can apply to make this work? Thank you.

Im curently rewriting old WebForms app to Blazor InteractiveServerApp (.NET 9). This app is reading and processing static template ".htm" files, that contains partial HTML markups and custom "internal" markups. For example how that file can look like:

MyCustomTemplate.htm

<table>
  <tr>
    <td>
      [MySpecialTagToBeTranslated]
    </td>
   </tr>
</table>

The idea is to give customers (implementation partners) ability to change the frontend freely as they need. The custom markup is then used to hide the complex code and binding logic behind "simpler" tag.

So, after processing that template, I have something like this:

ProcessingControl.razor

@((MarkupString)"<table><tr><td>")

@RenderProcessedControl (translates to "<label>My processed content</label>")

@((MarkupString)"</td></tr></table>")

@code {
// RenderFragment to build the controls
private RenderFragment RenderProcessedControl() => builder =>
{
    builder.OpenComponent<MySpecialTagToBeTranslated>(0);
    builder.CloseComponent();
    ... etc
};
}

The idea in this example is to have "My processed content" from component inside "" tag. But the final result in browser is:

<table><tr><td>

</td></tr></table>

<label>My processed content</label>

Is there a solution that I can apply to make this work? Thank you.

Share Improve this question edited Mar 10 at 1:32 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Mar 4 at 13:27 PetrKPetrK 233 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I've tested this by reducing your code to this MRE:

@page "/"

<PageTitle>Home</PageTitle>

@RenderWholeControl

@code {
    private RenderFragment RenderWholeControl => mybuilder =>
    {
        mybuilder.AddMarkupContent(0, """<div class="alert alert-primary">""");
        mybuilder.OpenElement(1, "h2");
        mybuilder.AddContent(2, "Hello Blazor");
        mybuilder.CloseElement();
        mybuilder.AddMarkupContent(3, "</div>");
    };
}

With the result:

It's appears to be specific to using "MarkupString". I think the Renderer is getting confused about which RenderFrame it's updating.

This needs reporting on to the AspNetCore Github repo to see what the team think of it.

How to get round it? I'm not sure as you haven't provided enough context. Build the markup using elements with the RenderTreeBuilder?

发布评论

评论列表(0)

  1. 暂无评论