用字符串合并Razor C#值

时间:2014-07-09 07:42:22

标签: c# asp.net-mvc-4 razor

我在像这样的元标记中有一些价值

<meta property="og:image" content="/Content/images/share-image.jpg"" />

问题在于Facebook希望标签是这样的

<meta property="og:image" content="http://www.example.com/Content/images/share-image.jpg"" />

我知道在MVC中如何获得这样的基本URL @ Request.Url.AbsoluteUri

问题是当我在视图中尝试这样的事情时

  <meta property="og:image" content="@Request.Url.AbsoluteUriContent/images/share-image.jpg"" />

我收到了错误,如何在视图中合并字符串

@Request.Url.AbsoluteUri

Content/images/share-image.jpg

1 个答案:

答案 0 :(得分:2)

尝试在C#语句周围使用大括号:

<meta property="og:image" content="@(Request.Url.AbsoluteUri)Content/images/share-image.jpg" />

这将明确告诉Razor解析器@()中的文本是C#代码。