Search code examples
.netasp.net-mvcrazorsyntax

When is the semicolon required in Razor syntax?


In one of my web MVC3 sites, I'm seeing a semicolon at the bottom of the page.

Are semicolons required on @using some.Library.Namespace; statements?


Solution

  • There are two rules for semicolons:

    1. Inside a code block, each complete code statement must end with a semicolon.

      <!-- Single-statement block -->
      @{ var theMonth = DateTime.Now.Month; }
      
      <!-- Multi-statement block -->
      @{
          var outsideTemp = 79;
          var weatherMessage = "Hello, it is " + outsideTemp + " degrees.";
      }
      
    2. Inline expressions don't end with a semicolon.

      <!-- Inline expression, so no semicolon -->
      <p>Today's weather: @weatherMessage</p>
      

    Further Reading