Is it possible to render blazor component as string so I can render static HTML. I know where is a way to render MVC view to string. What I need to do is to make a way to create static HTML page that will be downloaded to WPF application where user can then open it in browser? This is popular answer for MVC app: How to render an ASP.NET MVC view as a string? So is there anything similar for Blazor? I don't need to do it in Blazor but web is done in Blazor so it would be nice.
            Asked
            
        
        
            Active
            
        
            Viewed 3,008 times
        
    3
            
            
        - 
                    I believe that `RenderFragment` is what you need, Is this article, helpful? https://blazor-university.com/templating-components-with-renderfragements/ – StPaulis Oct 19 '20 at 08:40
- 
                    I just found out there is library called bUnit but I am not sure is it the best way to do it : https://github.com/egil/bUnit – Vlado Pandžić Oct 19 '20 at 08:47
- 
                    bUnit, is for testing. To pass string template to your component you need RenderFragment. – StPaulis Oct 19 '20 at 08:49
- 
                    Yes but it has method to get rendered fragment: var component = ctx.RenderComponent(); var html = component.Markup; – Vlado Pandžić Oct 19 '20 at 08:52
- 
                    In Blazor you can convert string, simple string, to HTML via RenderFragment, I don't think that another dependency is needed here. – StPaulis Oct 19 '20 at 08:55
- 
                    I will try it out,definitely – Vlado Pandžić Oct 19 '20 at 08:56
- 
                    @StPaulis - ever used a RenderFragment that way? – H H Oct 19 '20 at 10:25
- 
                    Related:; https://stackoverflow.com/q/68889811/1768303 – noseratio Aug 23 '21 at 13:13
1 Answers
2
            
            
        From Blazor .Net 5 RC1 https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-5-release-candidate-1/#blazor-webassembly-prerendering
in your hosting app you can use component tag helper with render-mode="Static" or render-mode="WebAssemblyPrerendered"
@page "/static-counter"
@namespace Server.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<component type="typeof(Client.Shared.MyComponent)" render-mode="WebAssemblyPrerendered" />
 
    
    
        JanB
        
- 248
- 1
- 11
