首页 > 解决方案 > 将可执行的 Blazor 代码嵌入到 JSON 输入字符串中

问题描述

我有以下 Blazor 代码,其中数据值是从 JSON 文件输入的。

<p>@product.Name</p>
<p>@((MarkupString)product.Description)</p>

我想做的是将@product.Name 嵌入到 JSON 文件中的描述文本中,以便将其呈现为描述的一部分。我正在寻找这样简单的东西:

"Description": "This a detailed description of the <dummytag>@product.Name</dummytag> product or service".

我尝试了各种组合,但无法渲染 gen3Product.Name。谁能告诉我如何做到这一点,拜托。我知道这会导致糟糕的安全结果。

标签: htmlrazorblazorblazor-server-side

解决方案


在 Blazor 中,@product.Name必须在部署之前进行编译。因此,将其嵌入您的数据中是行不通的。

但是,您可以使用旧式字符串格式:

 "Description": "This a detailed description of the <dummytag>{0}</dummytag> product or service"

接着

 <p>@((MarkupString) string.Format(product.Description, product.Name)</p>

但这显然不太灵活。


推荐阅读