How to render a Blazor component into an HTML string

天大地大妈咪最大 提交于 2021-01-22 00:31:33

问题


I'm looking for a way to render a Blazor component into an HTML string, so that I'll be able to use it as a templating engine to create and send emails in my web application. Ideas?


回答1:


Yes, you can use the test library provided by Steve Sanderson and adapt it to your needs.
This article explains how to use it : Introduction to Blazor Component Testing .
The library can be use to generate the HTML of a component.

exemple :

var host = new TestHost();
var component = host.AddComponent<YourComponent>();
var html = component.GetMarkup();

And you can inject services you need.

host.ConfigureServices(services => 
{
   service.AddSingleton<MyService>();  
});


来源:https://stackoverflow.com/questions/60437439/how-to-render-a-blazor-component-into-an-html-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!