Tutorials ASP.NET Core MVC Tutorial

Razor View Engine — Complete Guide

Razor View Engine — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ASP.NET Core MVC Tutorial on Toolliyo Academy.

On this page

ASP.NET Core MVC Tutorial · Lesson 37 of 200

Razor View Engine

Getting Started ✓Core MVC ✓Data & SecurityProductionCareer

Intermediate · 4 — Models, Routing & DI · ~6 min · Section 4: Views & Razor

What is this?

Razor lets you write HTML mixed with C#. @ starts C# code in a .cshtml file — like showing @Model.Name inside a paragraph.

Why should you care?

Server-rendered pages need dynamic data — prices, user names, loops over products. Razor is how MVC prints C# values into HTML.

See it live — copy this example

Create an MVC project (dotnet new mvc), add the code, and run dotnet run.

@model IEnumerable<ProductViewModel>

<h1>Products</h1>
<ul>
@foreach (var p in Model)
{
    <li>@p.Name — ₹@p.Price.ToString("N0")</li>
}
</ul>

Run Example »

Edit the code and click Run — like W3Schools Try it Yourself.

Code
Result

What happened?

  • @model declares the type Model will be.
  • @foreach is C# loop.
  • @p.Name outputs encoded HTML-safe text.
  • Razor compiles .cshtml to a C# class at runtime.

Try it yourself

  1. Open Views/Home/Index.cshtml and add @DateTime.Now.
  2. Create a list of strings in the controller, pass to view, loop with @foreach.
  3. View page source in browser — see rendered HTML without @ symbols.
  4. Change text or labels in the example and run again — watch the browser update.
  5. Break the code on purpose (remove a semicolon), read the error message, then fix it.