html - 将可执行的 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。谁能告诉我如何做到这一点,拜托。我知道这会导致糟糕的安全结果。
解决方案
在 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>
但这显然不太灵活。
推荐阅读
- pyspark - 具有可变范围的 PySpark 滚动操作
- javascript - 为什么我的下一个和上一个控件在分页时无法获取正确的索引?我需要做哪些改变?
- python - 如何使用 pytube、pafy 或其他模块在 Python 中从 YouTube 获取播放列表中每个视频的名称?
- java - 为什么 ThreadLocal 的 initialValue 不会增加我的变量?
- java - Eclipse 的 Package Explorer 树中的星号和加号是什么意思?
- android - 我正在尝试在 Android Native Activity 中使用 opengles 绘制形状。但是没有画出来
- kubernetes - K3S:如何通过 hub.docker.com 进行身份验证也拉取私有图像?
- javascript - 使用 MVC 的 JQueryFullCalendar
- xcode - 在 Xcode 12 中运行签名后脚本
- javascript - 获取相对于旋转 div 上 div 浏览器窗口的 x 和 y