Escape @ character in razor view engine

后端 未结 15 1827
梦谈多话
梦谈多话 2020-11-22 07:55

I am creating a sample ASP.NET MVC 3 site using Razor as view engine. The razor syntax starts with @ character e.g. @RenderBody(). If I write @test

相关标签:
15条回答
  • For the question about @RazorCodePart1 @@ @RazorCodePart2, you need to the sequence:

    @RazorCodePart1 @:@@ @RazorCodePart2
    

    I know, it looks a bit odd, but it works and will get you the literal character '@' between the code blocks.

    0 讨论(0)
  • 2020-11-22 08:31

    @@ should do it.

    0 讨论(0)
  • 2020-11-22 08:31

    @@ is the escape character for @ in Razor views as stated above.

    Razor does however try to work out when an '@' is just an '@' and where it marks C# (or VB.Net) code. One of the main uses for this is to identify email addresses within a Razor view - it should not be necessary to escape the @ character in an email address.

    0 讨论(0)
  • 2020-11-22 08:31

    I just had the same problem. I declared a variable putting my text with the @.

    @{
       var twitterSite = "@MyTwitterSite";
    }
    
    ...
    
    <meta name="twitter:site" content="@twitterSite">
    
    0 讨论(0)
  • 2020-11-22 08:31

    I know this question is old, but I tried all of the above and it didn't help me escape the character "@" in ASP.NET full framework (MVC 5) inside a URL. Based on Terje Solem's answer though, the UTF-8 code %40 worked for me. this is the original URL I was trying to reach:

    https://unpkg.com/@google/markerclustererplus@4.0.1/dist/markerclustererplus.min.js

    and with with the code in it:

    https://unpkg.com/%40google/markerclustererplus@4.0.1/dist/markerclustererplus.min.js
    
    0 讨论(0)
  • 2020-11-22 08:34

    @Html.Raw("@") seems to me to be even more reliable than @@, since not in all cases @@ will escape.

    Therefore:

    <meta name="twitter:site" content="@twitterSite">
    

    would be:

    <meta name="twitter:site" content="@Html.Raw("@")twitterSite">
    
    0 讨论(0)
提交回复
热议问题