Razor ViewEngine: How do I escape the “@” symbol?

北城以北 提交于 2019-11-27 21:45:32
JasCav

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 are adding Twitter meta tags

and your Twitter username is, say, blrbr ( follow me ;) )

it should look like this

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

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!