json - 如何在 ARM 模板中将参数与字符串值连接:无法解析模板语言表达式
问题描述
我需要连接 ARM 模板(JSON 文件)中的参数,但"/subscriptions/@{encodeURIComponent([parameters('id')])}/providers"
. 它看起来[parameters('id')]
不被识别为参数,它只是作为文本传递。
我知道可以使用union
表达式,但在我的情况下,我不需要只合并两个单独的参数(根据我的理解,这就是这样union
做的)。我需要构造一个字符串。
我想到的另一个选择是在 JSON 文件之外进行,以便我可以将其"/subscriptions/@{encodeURIComponent([parameters('id')])}/providers"
作为单个参数传递。但我发现这种方法无效,因为在我的 ARM 模板中,我有 10 个类似的变量,我只需要在其中“粘贴” id
.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"id": {
"type": "string"
}
},
"variables": {},
"resources": [{
...
"path": "/subscriptions/@{encodeURIComponent([parameters('id')])}/providers",
...
}]
}
解决方案
在 ARM 模板中,您可以使用concat和uriComponent函数来连接参数:
"[concat('/subscriptions/', uriComponent(parameters('id')), '/providers')]"
在您的示例中,它将类似于:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"id": {
"type": "string"
}
},
"variables": {},
"resources": [{
...
"path": "[concat('/subscriptions/', uriComponent(parameters('id')), '/providers')]",
...
}]
}
请参阅concat和uriComponent的文档。
推荐阅读
- excel - 具有自动保存功能的 VBA 循环;代码将执行第一个实例,但在永久之后生成空白
- wordpress - Revolution Slider 6 中是否具有“Helper Grid”功能?
- elasticsearch - 在 ElasticSearch 中使用 multi_match 查询的奇怪结果
- javascript - 在按钮单击 ASP.NET MVC 时调用操作方法
- javascript - 从 JSON 中获取下拉列表的动态值
- c# - 为什么这段代码会抛出“System.ArgumentOutOfRangeException”(SQlite)?
- java - 如何解决发布版本中的重复 jar 条目 [com/android/volley/R.class]?
- vue.js - 在一个组件中添加语言选项,但不添加标题
- python - 使用 Sklearn 库的 StandardScaler 在 Python 中缩放目标变量会出错
- vba - 使用 ole 自动化 (C++) 时如何获取对象类型