Chapter 2: WebPages Razor
WebPages Razor” (or in full: ASP.NET Web Pages – Adding Razor Code). This is the key chapter where they teach you how to actually add and write server-side code inside your .cshtml pages using Razor syntax.
Last time we talked about what ASP.NET Web Pages is. Today we zoom in on the most important part: adding Razor code.
I’ll explain it like we’re building it together on one screen — very detailed, with real examples copied/adapted from W3Schools style, plus my extra teacher explanations so you truly understand why and how it works.
Step 1: What does “Adding Razor Code” mean?
In a normal HTML file → everything is static.
In ASP.NET Web Pages → you add Razor code to make the page dynamic:
- Show current time/date
- Greet user by name
- Show different content based on time/choice
- Read from form, database, files
- Calculate things
- Loop over lists
Razor is the syntax (the rules) that lets you mix C# (or VB) server code smoothly into HTML.
Main idea: HTML stays HTML. Razor starts with @ and lives inside special blocks or inline.
Step 2: The Two Main Ways to Add Razor Code
There are two primary ways to write Razor code:
A. Code Blocks → for multiple lines / logic / variables B. Inline Expressions → for quick single values inside HTML
A. Code Blocks (the @{ … } or @Code … End Code part)
This is where you put “serious” code: declare variables, if-statements, loops, calculations — anything that doesn’t directly print.
For C# (most common in Web Pages tutorials):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
@{ // Everything here is pure C# — runs on server before HTML is sent var name = "Webliance"; var city = "Hyderabad"; var welcome = "Hello " + name + " from " + city + "! 🚀"; var hour = DateTime.Now.Hour; } |
For VB.NET (less common now, but still supported):
|
0 1 2 3 4 5 6 7 8 9 10 11 |
@Code Dim name = "Webliance" Dim city = "Hyderabad" Dim welcome = "Hello " & name & " from " & city & "! 🚀" Dim hour = DateTime.Now.Hour End Code |
Rules to remember (write these down!):
- C#: Use @{ … } — statements must end with ;
- VB: Use @Code … End Code — no ; needed
- Code inside runs first (server-side)
- Variables declared with var (C#) or Dim (VB)
B. Inline Expressions (the @ symbol magic)
This is the beautiful part — just put @ before something, and it prints the value right into HTML.
|
0 1 2 3 4 5 6 7 8 |
<p>Hello @name from @city!</p> <p>Current server time: @DateTime.Now</p> <p>Greeting: @welcome</p> |
- No quotes needed around variables
- Works for variables, properties, methods, calculations
- If complex → wrap in parentheses: @(hour + 5)
Step 3: Full Practical Example – Putting It All Together
Let’s build a complete page that adds Razor code in different ways.
File name: Welcome.cshtml
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
@{ // Code block: declare variables and logic var userName = "Webliance"; // you can change this var currentHour = DateTime.Now.Hour; string greeting; if (currentHour < 12) { greeting = "Good Morning"; } else if (currentHour < 17) { greeting = "Good Afternoon"; } else { greeting = "Good Evening"; } var message = greeting + ", " + userName + "! Welcome to your Razor lesson in Hyderabad."; } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Adding Razor Code - Example</title> </head> <body> <h1>@greeting, Webliance! 🌟</h1> <p>@message</p> <p>Right now the server clock shows: @DateTime.Now.ToString("dddd, MMMM dd, yyyy - hh:mm tt")</p> <p>Quick math example: 25 × 4 = @(25 * 4)</p> <p>Hour right now (24-hour format): @currentHour</p> <!-- Inline conditional (shorthand) --> <p>You are in @(currentHour >= 18 ? "evening mode" : "day mode")</p> </body> </html> |
What happens step-by-step (teacher whiteboard mode):
- Server sees .cshtml → hands to Razor engine
- Executes the @{ … } block first:
- Sets variables
- Checks time → decides greeting
- Then renders HTML
- Replaces every @something with the real value
- Browser gets pure HTML — no @ or C# visible!
Step 4: Common Ways People Add Razor Code (from W3Schools examples)
Here are the classic patterns you’ll see in the tutorial:
1. Simple variable output
|
0 1 2 3 4 5 6 7 |
@{ var msg = "Hello World"; } <p>@msg</p> |
2. Multi-line logic
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
@{ var price = 199; var tax = price * 0.18; var total = price + tax; } <p>Price: ₹@price</p> <p>Tax (18%): ₹@tax</p> <p>Total: ₹@total</p> |
3. Using built-in objects (no import needed)
|
0 1 2 3 4 5 6 7 |
<p>Today is @DateTime.Now.DayOfWeek</p> <p>Server name: @Request.ServerVariables["SERVER_NAME"]</p> |
4. Handling form input (very common!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@{ var result = ""; if (IsPost) { var num1 = Request["n1"].AsInt(0); var num2 = Request["n2"].AsInt(0); result = "Sum = " + (num1 + num2); } } <form method="post"> Number 1: <input type="number" name="n1" /><br> Number 2: <input type="number" name="n2" /><br> <input type="submit" value="Calculate" /> </form> <p>@result</p> |
Step 5: Quick Teacher Tips & Gotchas (2026 edition)
- Always end C# statements with ;
- Use @: or <text> if you want to output plain text inside a code block (rare)
- Razor auto-encodes output → safer than Classic ASP (prevents XSS)
- Prefer C# over VB nowadays — more jobs, better modern support
- In 2026: This exact syntax lives on in ASP.NET Core Razor Pages — so learning it now is future-proof!
Got questions?
- Want to see loops (@for, @foreach) next?
- Conditionals with @if inside HTML?
- How to escape @ if you really need to show “@” symbol?
- Or a full page with form + database?
Just say — next lesson is ready whenever you are! You’re doing awesome, keep going! 🚀🇮🇳
