将 Razor 数据插入 src 和 href 属性

Getting Razor data inserted into src and href attributes

我需要将一个 API 键和一个位置字符串插入到我的 iframe 元素的 src 属性中。

这是我的 iframe 在 atm 中的样子:

<iframe class="center-block" src="https://www.google.com/maps/embed/v1/place?key=@Umbraco.Field('aPIKey')&q=@Umbraco.Field('lokation')" width="95%" height="300" frameborder="0" style="border:0" allowfullscreen></iframe>

但是当我转到该页面时,出现此错误:

Too many characters in character literal

我尝试使用“”代替 razor 属性,但这会破坏文档其余部分的格式。

我真的很困惑在 HTML 属性中使用 Razor 语法的正确方法是什么。

@Umbraco.Field('lokation') @Umbraco.Field('aPIKey')

应该是

@Umbraco.Field("lokation") @Umbraco.Field("aPIKey")

' 引号用于 c#

中的字符文字

现在因为您在剃须刀中使用 " 引号,您需要将 html 引号更改为 '.

<iframe class="center-block" src='https://www.google.com/maps/embed/v1/place?key=@Umbraco.Field("aPIKey")&q=@Umbraco.Field("lokation")' width="95%" height="300" frameborder="0" style="border:0" allowfullscreen></iframe>

它应该是 @(Umbraco.Field("lokation")) - 如果您的 "variable" 包含括号或其他特殊字符,例如 " 当您已经在以 "" 分隔的文本字符串中时,您需要将整个事情在......更多parantheses :-)