1
Vote

Additional '@' characters in Razor code blocks break compilation in Razor v2.

description

  1. In Visual Studio, create a Razor v2 web site.
  2. Copy and paste the following code into one of the content pages:
@{
//Check there are slider image page loaded
var theCount = @Model.Descendants("SliderImagePage").Count(); if (theCount > 0)
{
    foreach (var theImagePage in Model.Descendants("SliderImagePage"))
      {
       var theImage = theImagePage.Media("sliderImage","umbracoFile");
       if (theImagePage.IsFirst()) 
       {
        @:<div class="slide" style="background-image:url('@Html.Raw(theImage)');display:block;"></div>
        } else {
        @:<div class="slide" style="background-image:url('@Html.Raw(theImage)');display:none;"></div>
        }
      }    
 }
else
{
@: No Picture Image pages set up
}
}

(Note: The above code compiles without error in Razor v1.)
  1. Attempt to build the site.
Result: The site will fail to compile with multiple errors. Scrutinizing the errors , reporting that the @: expression on line 11 requires a keyword, identifier, or string, and that the subsequent '}' character on line 12 closes the code block that began on line 1. If a user removes the "@" character on line 3 of the code snippet, the compilation proceeds without error.
Expected: The code snippet above should compile as it would have in Razor v1.

comments

yishaigalatzer wrote Apr 22 at 9:51 PM

The at the beginning was not ever valid, it just slipped through validation in V1.