Key Concepts

Review core concepts you need to learn to master this subject

Razor Syntax

@page @model IndexModel <h2>Welcome</h2> <ul> @for (int i = 0; i < 3; i++) { <li>@i</li> } </ul>

Razor Syntax allows you to embed code (C#) into page views through the use of a few keywords (such as “@”), and then have the C# code be processed and converted at runtime to HTML. In other words, rather than coding static HTML syntax in the page view, a user can code in the view in C# and have the Razor engine convert the C# code into HTML at runtime, creating a dynamically generated HTML web page.

Razor Pages Syntax I
Lesson 1 of 2
  1. 1
    Wouldn’t it be great if you could get started creating a web application by just running a few commands? Well, that’s possible now with Razor Pages!! When it comes to building web applications w…
  2. 2
    So how does a view page in a Razor app work? In order for a file to behave as a Razor view page, the first line in the file must be @page. The @page directive indicates that the file is a Razor Pag…
  3. 3
    All C# expressions are preceded with the character “@“. For example: @DateTime.Now.ToShortDateString() If your C# code needs spaces, then it must be wrapped in parentheses: Last week …
  4. 4
    You can use the if statement in your Razor code pretty much the same way you would in regular C# code — just prefix the keyword with the “@“ sign: @{ int value = 4; } @if (value % 2 =…
  5. 5
    Similar to if statements, switch cases operate the same way — by prefixing the keyword with the “@“ sign: @{ int number = 2 } @switch (number) { case 1: The value is 1! break; case …
  6. 6
    Let’s not forget about loops! There are a number of different types of loops in C# that can be written in Razor syntax. Let’s say we have a list of names we’ll be looping over: @{ List names …
  7. 7
    Great job! We learned a lot in this lesson. We were able to re-create certain HTML markup using Razor syntax and saw how Razor Pages can be useful by reducing repetitive code. Let’s summarize wha…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo