Razor ViewEngine: 如何转义“@”符号?

我正在尝试在 ASP.NET MVC3中输出一些 Twitter 句柄以及 Twitter@Anywhere API,但是我还没有弄明白如何在 Razor 视图中实际转义“@”符号。

有人知道在 Razor 中转义“@”字符的语法吗?

我试过使用 <text></text>,结果是出现 JIT 错误。

53373 次浏览

You have to use @@ to escape the @ symbol.

One important thing to notice is that you DO NOT need to escape the @ symbol when it exists within an email address. Razor should be smart enough to figure that out on its own.

if you need to comment @ symbols in css code and just @@ wont work, use this:

@("@@font-face"){ ... css ...}

I had yet another odd case: pass '@' + @Model.SomeProperty to a link href.

The best solution for this case was given IMO in this answer to a similar question. Both @@ and @: didn't work while using the html code &#64; would complicate things.

So, my code was <a href="~/path/?query=@('@')@Model.SomePropery">

@Html.Raw('@')@Model.SomePropery would have worked as well.

If you are adding Twitter meta tags

and your Twitter username is, say, foobar

it should look like this

<meta name="twitter:site" content=@("@foobar")>

If you are adding Schema.org JSON-LD Tags, @Matt's answer worked for me:

  ....
@("@context"): "https://schema.org",
....