Views & Razor Syntax
Learn Razor syntax for mixing C# and HTML to build dynamic views in ASP.NET Core MVC.
What is Razor?
Razor is a syntax that lets you embed C# directly inside HTML using the `@` symbol, used to build dynamic `.cshtml` view files in ASP.NET Core MVC.
Embedding C# in HTML
Views/Home/Index.cshtml
<h1>Welcome, @Model.Name!</h1><p>Today is @DateTime.Now.ToShortDateString()</p>Loops and Conditions in Views
@if (Model.Tasks.Count == 0){ <p>No tasks yet!</p>}else{ <ul> @foreach (var task in Model.Tasks) { <li>@task</li> } </ul>}Returning a View from a Controller
public class HomeController : Controller{ public IActionResult Index() { var model = new HomeViewModel { Name = "Priya" }; return View(model); // looks for Views/Home/Index.cshtml }}View Discovery by Convention
View(model) automatically looks for a view matching the controller and action name — Views/{Controller}/{Action}.cshtml — unless you specify a different path explicitly.
FAQs
No — pure Web APIs return JSON directly and skip Razor entirely. Razor matters for MVC apps and Blazor.
Summary
- Razor lets you embed C# in HTML using @.
- Loops and conditionals work naturally inside a view.
- Controller's View(model) renders a .cshtml file matching that action by convention.